2011-09-09 35 views
0
<?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 
    ?> 

上面的代码给出了输出sandesh。关于Strtok在PHP中的疑问

但是如果我想输出“评论国际技术学院”,那么我应该如何修改上面的代码。

感谢和问候 Sandesh

回答

2
<?php 
$string = "sandesh commented on institue international institute of technology"; 
echo substr($string,strpos($string," ")+1); 

文档

编辑

我实际需要的第四代币

<?php 
$string = "sandesh commented on institue international institute of technology"; 
$str_array = explode(" ",$string); 
$str_array = slice($str_array,4); 
echo implode(" ",$str_array); 
+0

喜,在这里,我真正需要的字符串的第四代币后,对于我可以重复这一过程,4倍,有什么办法直接我可以得到第四个标记后面的字符串,而不用放入循环。 –

+0

@Sandesh:编辑,添加解决方案,以满足您的需求 –

1

后的字符串,因为您是根据空间符号化,strtok给你第一个单词。下一次你打电话给strtok,你会得到第二个单词,等等。根据你提供的字符串和你提供的令牌,将字符串的其余部分作为一个单独的令牌,是没有办法的。

+0

谢谢..我知道原因,我想它会记住指针。 –

+0

是的;它和C的strtok完全一样。它会标记您提供的字符串,然后循环执行任何操作。 –

1

有没有什么办法直接我可以得到第四个标记后的字符串, 没有进入循环。

这将得到一个通过第四个空格后的字符串。

<?php 
$string = "sandesh commented on institue international institute of technology"; 
preg_match('/(?:.*?\s){4}(.*)/', $string, $m); 
$new_string = $m[1]; 
echo $new_string; 
?> 

输出

技术国际研究所