Hướng dẫn php str_replace multiple

I remember doing this before, but can't find the code. I use str_replace to replace one character like this: str_replace(':', ' ', $string); but I want to replace all the following characters \/:*?"<>|, without doing a str_replace for each.

asked Sep 30, 2011 at 2:51

2

Like this:

str_replace(array(':', '\\', '/', '*'), ' ', $string); 

Or, in modern PHP (anything from 5.4 onwards), the slighty less wordy:

str_replace([':', '\\', '/', '*'], ' ', $string); 

Hướng dẫn php str_replace multiple

Codemonkey

4,2124 gold badges38 silver badges72 bronze badges

answered Sep 30, 2011 at 2:53

DogbertDogbert

205k40 gold badges384 silver badges392 bronze badges

0

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

answered Sep 30, 2011 at 2:54

NullUserExceptionNullUserException

82.1k27 gold badges205 silver badges230 bronze badges

6

For example, if you want to replace search2 with replace1 and search2 with replace2 then following code will work:

print str_replace( array("search2","search2"), array("replace1", "replace2"), "search2 search2" ); 

// Output: replace1 replace2

answered Feb 10, 2015 at 20:36

SumoanandSumoanand

8,6651 gold badge47 silver badges46 bronze badges

0

str_replace( array("search","items"), array("replace", "items"), $string ); 

answered Sep 30, 2011 at 2:54

Hướng dẫn php str_replace multiple

MartyMarty

38.6k19 gold badges91 silver badges162 bronze badges

If you're only replacing single characters, you should use strtr()

answered Sep 30, 2011 at 3:05

Explosion PillsExplosion Pills

185k49 gold badges318 silver badges394 bronze badges

1

You could use preg_replace(). The following example can be run using command line php:

|"; $s2 = preg_replace("^[\\\\/:\*\?\"<>\|]^", " ", $s1) ; echo "\n\$s2: \"" . $s2 . "\"\n"; ?> 

Output:

$s2: "the string          "

answered Sep 30, 2011 at 2:54

GreenMattGreenMatt

17.8k7 gold badges50 silver badges76 bronze badges

1

I had a situation whereby I had to replace the HTML tags with two different replacement results.

$trades = "
  • Sprinkler and Fire Protection Installer
  •  
  • Steamfitter
  •  
  • Terrazzo, Tile and Marble Setter
  • "; $s1 = str_replace('
  • ', '"', $trades); $s2 = str_replace('
  • ', '",', $s1); echo $s2; 

    result

    "Sprinkler and Fire Protection Installer", "Steamfitter ", "Terrazzo, Tile and Marble Setter",

    answered Feb 15, 2017 at 18:19

    I guess you are looking after this:

    // example private const TEMPLATE = __DIR__.'/Resources/{type}_{language}.json'; ... public function templateFor(string $type, string $language): string { return \str_replace(['{type}', '{language}'], [$type, $language], self::TEMPLATE); } 

    answered Oct 2, 2018 at 11:20

    Hướng dẫn php str_replace multiple

    In my use case, I parameterized some fields in an HTML document, and once I load these fields I match and replace them using the str_replace method.

     

    answered Apr 5, 2020 at 19:55

    dataviewsdataviews

    1,8804 gold badges17 silver badges54 bronze badges