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: asked Sep 30, 2011 at 2:51 2 Like this:
Or, in modern PHP (anything from 5.4 onwards), the slighty less wordy:
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
Alternatively you could use
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:
answered Feb 10, 2015 at 20:36 SumoanandSumoanand 8,6651 gold badge47 silver badges46 bronze badges 0
answered Sep 30, 2011 at 2:54 MartyMarty 38.6k19 gold badges91 silver badges162 bronze badges If you're only replacing single characters, you should use 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:
Output:
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.
Steamfitter
Terrazzo, Tile and Marble Setter
', '"', $trades); $s2 = str_replace('
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:
answered Oct 2, 2018 at 11:20 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 |
