<?php
$string = "sandesh commented on institue international institute of technology";
/* Use tab and newline as tokenizing characters as well */
$tok = strtok($string, " \n\t");
echo $tok
?>
The above code gives me output sandesh.
But开发者_C百科 if I want the output "commented on institute international institute of technology", then how should I modify the above code.
Thanks and Regards Sandesh
<?php
$string = "sandesh commented on institue international institute of technology";
echo substr($string,strpos($string," ")+1);
documentation
- http://php.net/manual/en/function.strpos.php
- http://php.net/manual/en/function.substr.php
Edit
i actually need the string after the fourth token
<?php
$string = "sandesh commented on institue international institute of technology";
$str_array = explode(" ",$string);
$str_array = slice($str_array,4);
echo implode(" ",$str_array);
- http://php.net/manual/en/function.explode.php
- http://www.php.net/manual/en/function.implode.php
- http://php.net/manual/en/function.array-slice.php
Because you're tokenizing based on space, strtok
gives you the first word. The next time you call strtok, you'll get the second word, etc. There is no way, given the string that you have and the token that you supply, to get the rest of the string as a single token.
is there any way directly i can get the string after fourth token , without putting into loop.
This will get the string after the fourth space in one pass.
<?php
$string = "sandesh commented on institue international institute of technology";
preg_match('/(?:.*?\s){4}(.*)/', $string, $m);
$new_string = $m[1];
echo $new_string;
?>
Output
international institute of technology
精彩评论