2011-08-13 35 views
1

使用针分割字符串有很多功能,最聪明(最短)的方法是什么?需要

$haystack="/this/is/the/haystack/string/to/be/trimmed/using/a/needle/string/"; 
$needle="to/be/"; 

结果是

/这/是/中/干草堆/串/

回答

1

我会做这样的,它不一定是最短方式(我确信有更短的方式),但它是相当简洁,可读和高效的:

$newStr = substr($haystack, 0, strpos($haystack, $needle)); 

Demo.