abdfahim Posted August 21, 2006 Share Posted August 21, 2006 I had the following code[code]$test="This is the string";echo $test;[/code]Now I want to echo the first 3 words (or say, first 9 characters) of $test, that is I want to show "This is the" only. How can i do that? Is there any character count and trim method? Link to comment https://forums.phpfreaks.com/topic/18162-solved-show-first-few-character-of-a-string/ Share on other sites More sharing options...
logu Posted August 21, 2006 Share Posted August 21, 2006 use substring funcitonecho substr($test, 0, 9); Link to comment https://forums.phpfreaks.com/topic/18162-solved-show-first-few-character-of-a-string/#findComment-77880 Share on other sites More sharing options...
tomfmason Posted August 21, 2006 Share Posted August 21, 2006 or[code=php:0]list($a, $b, $c) = explode(" ", $test);echo "$a, $b, $c";[/code]Hope this helps Link to comment https://forums.phpfreaks.com/topic/18162-solved-show-first-few-character-of-a-string/#findComment-77881 Share on other sites More sharing options...
Jervous Posted August 21, 2006 Share Posted August 21, 2006 To return the first 9 characters:[code]$test = "This is the string";$output = substr($test, 0, 8);echo $output;// This is t?>[/code]It doesn't return "he" because PHP counts whitespace as a character.EDIT: I accidentally pressed Preview so the reply never got sent. Also, you would use 0 and 8, not 9, because 0 - 8 is 9, 0 - 9 is 10. Link to comment https://forums.phpfreaks.com/topic/18162-solved-show-first-few-character-of-a-string/#findComment-77886 Share on other sites More sharing options...
abdfahim Posted August 21, 2006 Author Share Posted August 21, 2006 You guys are simply GREAT!!! Thanx all. Link to comment https://forums.phpfreaks.com/topic/18162-solved-show-first-few-character-of-a-string/#findComment-77919 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.