Kingy Posted January 17, 2008 Share Posted January 17, 2008 Ok i have 3 varibles that i want to join together and then i want it to count them. <?php $string = "$ex[3] "." $ex[4] "." $ex[5]"; $string = trim($string); $length = strlen($string); echo $string; echo $length; ?> if i use the example `string hello my name is kingy it returns `string hello my name is kingy 32 But if you count, there should only be 30 characters :s Quote Link to comment https://forums.phpfreaks.com/topic/86388-solved-string-length/ Share on other sites More sharing options...
Barand Posted January 17, 2008 Share Posted January 17, 2008 You could have extra spaces btween the words. trim() only removes from the ends. Quote Link to comment https://forums.phpfreaks.com/topic/86388-solved-string-length/#findComment-441454 Share on other sites More sharing options...
Kingy Posted January 17, 2008 Author Share Posted January 17, 2008 oh yeah i think i see now the output is actually String: `string hello my name is kingy 2 spaces either side of hello... how do i fix this :s `string = $ex[3] hello = $ex[4] my name is kingy = $ex[5] so i have stuffed up my spaces in between the $ex somehow Quote Link to comment https://forums.phpfreaks.com/topic/86388-solved-string-length/#findComment-441459 Share on other sites More sharing options...
revraz Posted January 17, 2008 Share Posted January 17, 2008 Change the spaces here $string = "$ex[3] "."$ex[4] "."$ex[5]"; Quote Link to comment https://forums.phpfreaks.com/topic/86388-solved-string-length/#findComment-441461 Share on other sites More sharing options...
The Little Guy Posted January 17, 2008 Share Posted January 17, 2008 Try this: <?php $string = "$ex[3] "." $ex[4] "." $ex[5]"; $string = trim($string); $exp = explode(" ",$string); $len = 0; foreach($exp as $val){ $len += strlen($val); } echo $string; echo $len; ?> Quote Link to comment https://forums.phpfreaks.com/topic/86388-solved-string-length/#findComment-441462 Share on other sites More sharing options...
Kingy Posted January 17, 2008 Author Share Posted January 17, 2008 thanks, its all sorted. cheers all Quote Link to comment https://forums.phpfreaks.com/topic/86388-solved-string-length/#findComment-441478 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.