Question

I need to replace words from text

    $test = "Nordman Nokian NORDMAN 4 XL";
$replaces = array("Amtel " ,"Bridgestone " ,"Continental " ,"Cordiant " ,"Dunlop " ,"Formula " ,"Gislaved " ,"Kingstar " ,"BFGoodrich " ,"GoodYear " ,"Hankook " ,"Headway " ,"Fulda " ,"HIFLY " ,"Kumho " ,"Gremax " ,"Matador " ,"Michelin " ,"Rosava " ,"Nankang " ,"Nokian " ,"Nordman Nokian " ,"Pirelli " ,"Radial " ,"Sava " ,"Tigar " ,"Toyo " ,"Viatti " ,"Maxxis " ,"Yokohama ");
echo str_replace($replaces, "", $test);
// return Nordman NORDMAN 4 XL

the desired result

// return NORDMAN 4 XL

I apologize for the change in question, added the code which is not working replacement

Was it helpful?

Solution

Your problem is you have the string Nokian being replaced before the string Nordman Nokian. Because the single word Nokian is being matched first, the string is being transformed to Nordman NORDMAN 4 XL, and str_replace is unable to match the string Nordman Nokian. Just switch the order of your array items

I.E. ..."Nordman Nokian ","Nokian "...

OTHER TIPS

You have typo in your array:

$replaces = array("Hellow world","Like");

Now the str_replace returns this

Refer - http://in1.php.net/manual/en/function.str-replace.php

Here, the array is the words we want to replace, second parameter is the string we want to replace with and the third param is the haystack of string. This should work

// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top