Str_Replace for Multiple Items

How to replace multiple items from a text string in PHP?

You can pass arrays as parameters to str_replace(). Check the manual.

// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = ["fruits", "vegetables", "fiber"];
$yummy = ["pizza", "beer", "ice cream"];

$newPhrase = str_replace($healthy, $yummy, $phrase);

Str_replace for multiple items

str_replace() can take an array, so you could do:

$new_str = str_replace(str_split('\\/:*?"<>|'), ' ', $string);

Alternatively you could use preg_replace():

$new_str = preg_replace('~[\\\\/:*?"<>|]~', ' ', $string);

how to Str_replace for multiple items

You can use strtr() and an associative array to do this:

<?php
$text = "Text about -- and -+- !";
$replacements = [
"--" => "/",
"-+-" => ":",
];
echo strtr($text, $replacements); // Text about / and : !

To add more replacements, simply keep adding more elements to the $replacements array. Index is the string to look for, value is the replacement.

  • Demo
  • strtr() reference

replacing multiple items in a string with str_replace

You can use strtr to do multiple replaces simultaneously:

strtr($test, array('_' => ' ', ',' => ', '));

Replace multiple words with multiple parameters - str_replace()

Do it like this

$str="We have received [MACHINE_NAME] for service. Your service request id is [NEW_SERVICE_ID]. Thank you.";
$remove = array("[MACHINE_NAME]", "[NEW_SERVICE_ID]");
$add = array("YOURMACHINE_NAME", "SERVICE_ID");
echo $onlyconsonants = str_replace($remove, $add,$str );

Search and replace multiple values with multiple/different values in PHP5?

You are looking for str_replace().

$string = 'blah blarh bleh bleh blarh';
$result = str_replace(
array('blah', 'blarh'),
array('bleh', 'blerh'),
$string
);

// Additional tip:

And if you are stuck with an associative array like in your example, you can split it up like that:

$searchReplaceArray = array(
'blah' => 'bleh',
'blarh' => 'blerh'
);
$result = str_replace(
array_keys($searchReplaceArray),
array_values($searchReplaceArray),
$string
);

str_replace for multiple items then store results in new variable

You can accomplish this by means of a regular expression:

preg_match_all('/(?<=@)(\w){1,15}/', $content, $results);

which will store this array in the variable $results:

[
[
"steve",
"dave",
],
]

and you could enumerate the matches by looping over $results[0]:

foreach($results[0] as $name) {
echo $name . '<br>';
}

prints:

steve
dave

If you're curious about what /(?<=@)(\w){1,15}/ means:

(?<=@) - "lookbehind" - this means we need a @ to precede what we're actually interested in matching

(\w){1,15} means match a word with a maximum length of 15 (the max size of a twitter name)

so together we're matching the twitter username that follows an @ sign.

PHP replace multiple value using str_replace?

From the manual page for str_replace():

Caution

Replacement order gotcha

Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document.

For example, "m" is replaced with "Month", then the "h" in "Month" is replaced with "Hours", which comes later in the array of replacements.

strtr() doesn't have this issue because it tries all keys of the same length at the same time:

$date = strtr($key, array(
'y' => 'Year',
'm' => 'Month',
'd' => 'Days',
'h' => 'Hours',
'i' => 'Munites', // [sic]
's' => 'Seconds',
));


Related Topics



Leave a reply



Submit