开发者

Removing a duplication of a variable in a string?

开发者 https://www.devze.com 2023-01-25 06:36 出处:网络
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

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 ou开发者_如何学运维t 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'.


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


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消