Faks
Faks

Reputation: 416

PHP regex find word (even with double letters) then replace it

I'm using PHP to create a string filter. I need to replace some words by other words so I'm using the str_replace() function like this :

$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
// echo -> "You should eat pizza, beer, and ice cream every day.";

So far so good but I need to replace words even if they contains several letters like this:

$phrase  = "You should eat fruuits, vegetaaables, and fiiibeeer every day.";

I found this example to remove double letters in a string:

$string = preg_replace('/(\w)\1+/', '$1', $phrase);

But if I apply this example to my string, all words with double letters will be changed... For example "Google" will become "Gogle" and I don't want that.

Any ideas ? Thanks!

Upvotes: 0

Views: 510

Answers (3)

Federico Piazza
Federico Piazza

Reputation: 31025

If you want specific words then you can use multiple patterns in the preg_replace.

For your example you can use multiple patterns like this:

$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$patterns = array("/fru+its/", "/vegeta+bles/", "/fi+be+r/");
$yummy   = array("pizza", "beer", "ice cream");
$newphrase = preg_replace($patterns, $yummy, $phrase);

In case you want to detect only fruits, vegetables and fiber, then you could use:

$patterns = array("f+r+u+i+t+s+", "v+e+g+e+t+a+b+l+e+s+", "f+i+b+e+r+");

Regex demo

IdeOne code example

Upvotes: 0

fsn
fsn

Reputation: 539

Working example

$word =array('fiber', 'milk', 'nuts'); // words to scan for
$phrase = "You should eat fibeeer, nuuuuts, and milkkk every day."; //phrase
$filter = array(); // array of dynamicly generated filters

foreach($word as $name){ // loop for filter generating
    $temp = str_split($name);
    $temp = implode('{1,}', $temp). '{1,}';
    $filter[$name] = $temp;
}


$phraseTable = explode(' ', $phrase); // getting phrase into an array

foreach($phraseTable as &$data){ // scanning phrase
    foreach($filter as $name => $value){
         if(  preg_match( '/'.$value.'[,.]?/', $data)){
             $data = $name;
    }  
    } 
}       

$phraseScanned = implode(' ', $phraseTable); // imploding formatted phrase
echo $phraseScanned;  // echo result

Upvotes: 0

chris85
chris85

Reputation: 23880

I'd do something like this

$phrase  = "You should eat suck, sck, and suuucky every day.";
$healthy = 's+u*c+k+';
$yummy = 'luck';
$newphrase = preg_replace('/' . $healthy . '/', $yummy, $phrase);
echo $newphrase;

Output:

You should eat luck, luck, and lucky every day.

The + is quantifier meaning one or more of the previous character. The * is a zero or more quantifier meaning the character doesn't have to be present but if it is any number of that character can be present.

Demo: https://regex101.com/r/sN5sZ6/1

Also with ass word boundaries should be used because you don't want to be replacing class etc. https://regex101.com/r/oM4wY1/2 Kinda becomes a winding road though..

Upvotes: 1

Related Questions