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? Quote Link to comment Share on other sites More sharing options...
logu Posted August 21, 2006 Share Posted August 21, 2006 use substring funcitonecho substr($test, 0, 9); Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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.