假设你有这样一个字符串:
$string = 'hello my name is blah blah and wats yours';你想检查所有可能有复制品的地方..不是任何单词,而是本例中所选的单词“blah”。
$variable = 'blah';也就是说,如果“blah”背靠背出现--删除其中一个。
我考虑将字符串拆分成一个数组,如果数组中的一个变量以相同的单词开头,则最后一个变量以相同的单词结尾,然后去掉一个变量并重新构建字符串。看起来很繁琐,所以这就是为什么我要问是否有更简单的方法。
有什么想法吗?
编辑:我刚刚意识到我没有考虑做一个简单的‘preg_match’和'blah‘。
发布于 2010-11-17 18:17:54
$string = 'hello my name is blah blah and wats yours';
$search_string = 'blah';
$first_occurence = strpos($string , $search_string);
If (int preg_match($search_string , $string ) > 1) {
echo "String found more than once!!!";
// remove all occurences of searchstring from string excepte the first one
$string = substr ($string, 0, $first_occurence +1) . str_replace($search_string, '', substr ($string,$first_occurence +1));
}https://stackoverflow.com/questions/4203360
复制相似问题