Using an array in preg_replace to reduce codes

frosty

Is it possible to use an array in preg_replace to reduce the codes needed? So, something like this:

$text = "1 2 3";
$array = array(1 => "one", 2 => "two", 3 => "three");

preg_replace($array, $text);

echo $text;

Result:

1 2 3

Needed Result:

one two three
bobble bubble

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject,... preg_replace

$text = "1 2 3";

$array = array(
'/1/' => "one",
'/2/' => "two",
'/3/' => "three");

$text = preg_replace(array_keys($array), $array, $text);

Demo at eval.in (as mentioned in the comments, there is no need to use regex here)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using preg_replace on an array

From Dev

When using preg_replace on a variable in an array, the variable doesn't reflect preg_replace changes

From Dev

preg_replace with array index

From Dev

How to replace substrings with strings from array using preg_replace? (PHP)

From Dev

Issue in using Preg_replace

From Dev

PHP - preg_replace to change emoticons codes for emoticons images

From Dev

preg_replace to preg_replace_callback with an array as replacement

From Dev

preg_replace to preg_replace_callback with an array as replacement

From Dev

preg_replace remove [] around array

From Dev

php preg_replace() indexed array issue

From Dev

Replacing array variables in string with preg_replace

From Dev

Replacing array variables in string with preg_replace

From Dev

Php preg_replace with array in elements

From Dev

Using each match in preg_replace() in PHP

From Dev

Removing <script> tags using preg_replace

From Dev

Using preg_replace to alter youtube links

From Dev

Converting a string using preg_replace

From Dev

Using preg_replace to "update" searched string

From Dev

Using preg_replace pattern to clean a string

From Dev

Error using preg_replace function

From Dev

Using back reference in preg_replace in php

From Dev

Remove  from string using preg_replace

From Dev

Using preg_replace to alter youtube links

From Dev

remove certain things using preg_replace

From Dev

Using preg_replace with career names and failing at it

From Dev

Using preg_replace to transform URLs in a string

From Dev

Using preg_replace in PHP - Strange Behaviour

From Dev

Using preg_replace in foreach loop issue

From Dev

php preg_replace replace array name from input

Related Related

HotTag

Archive