Reputation: 11240
Say you have a string like:
$string = 'hello my name is blah blah and wats yours';
And you wanted to go through and check for any places where there might be a duplication.. not of any word, but just of the chosen word in this instance 'blah'.
$variable = 'blah';
Ie, if 'blah' appears back to back - remove one of them.
I consider splitting the string into an array and if one variable in the array starts with the same word the last one ended with then cut out one and rebuild the string. Seems tedius so this is why I am asking if there might be a simpler way.
Any ideas?
edit: i just realised i didn't consider doing a preg_match of simply 'blah blah' with 'blah'.
Upvotes: 2
Views: 113
Reputation: 10341
How about this:
$string = 'hello my name is blah blah and wats yours';
$variable = 'blah';
$string = preg_replace( '/(\b'.$variable.'\s+){2,}/' , '\1' , $string );
Allows for more than one variable to be handled (ie you could either loop through a number of $variable
s or you could create an array of them and use a single preg_replace()
call.
Or just use a str_replace()
$string = 'hello my name is blah blah and wats yours';
$variable = 'blah';
$string = str_replace( $variable.' '.$variable.' ' , $variable.' ' , $string );
Upvotes: 3
Reputation: 237845
All the answers so far talk about any duplication of the word in the string. I think you are only looking to remove consecutive identical words. You can do so with preg_replace
:
$string = 'hello my name is blah blah and wats yours';
$string = preg_replace('/\b(\w+)(\s+)\\1\s*/', '\\1\\2', $string);
Note that this function is rather stupid, and so will remove valid phrases like "had had". You could probably work around this using a whitelist and preg_replace_callback
.
Just re-read your question and previously missed the bit about "specified words only". You can do this with a blacklist of words that must not be duplicated:
$string = 'hello my name is blah blah and wats yours. I had had a bad day';
$string = preg_replace_callback('/\b(\w+)(\s+)\\1\s*/i', function($matches) {
$blacklist = array ('blah');
if (in_array(strtolower($matches[1]), $blacklist)) {
return $matches[1] . $matches[2];
} else {
return $matches[0];
}
}, $string);
// $string == "hello my name is blah and wats yours. I had had a bad day"
You could add more than one word to the $blacklist
array.
Upvotes: 1
Reputation: 50832
$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));
}
Upvotes: 1
Reputation: 10714
You could check the position of every occurence of $variable in $string, and check if the positions of several occurences just differs in the length of $variable (maybe + whitespaces).
Upvotes: 1