How to replace some characters in a string?

Suppose I have a line like this

SOMETHING [1000137c] SOMETHING = John Rogers III [SOMETHING] SOMETHING ELSE 

and i need to include it in this

 SOMETHING [1000137c] SOMETHING = John_Rogers_III [SOMETHING] SOMETHING ELSE 

Therefore, I need to replace the spaces with the words "_" between the words after "[1000137c] SOMETHING =" and before "[". How can i do this in php?

Thanks!

+4
source share
3 answers
 $s = "SOMETHING [1000137c] SOMETHING = John Rogers III [SOMETHING] SOMETHING ELSE"; $a = split(" = ",$s,2); $b = split(' \[',$a[1],2); $s = $a[0] . ' = ' . strtr($b[0],' ','_') . ' [' . $b[1]; print_r($s); 

gives:

 SOMETHING [1000137c] SOMETHING = John_Rogers_III [SOMETHING] SOMETHING ELSE 
+3
source

$ arr = break the string in the array into "=" and then

 str_replace(" ", "_", $arr[1]) 
0
source

using a regular expression like this: / ^ [\ w] + [[\ w \ d] +] [\ w] + = ([\ w \ d] +) [[\ w \ d] +] [\ w] + $ / i "should return a match of 1 as" John Rogers III ", although this is based on the current example.

using preg_replace_callback with the above expression, you can str_replace replace spaces with underscores in the callback function.

0
source

Source: https://habr.com/ru/post/1311181/


All Articles