سؤال

<?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 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

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);

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top