regex - Regular Expression to remove forward slashes from words not numbers in PHP -


i trying remove forward slashes separating words, not numbers in php.

example string:

"sf/berkeley paso/slo/sb on 9/25" 

i result be:

"sf berkeley paso slo sb on 9/25" 

using "/([a-z]+)\\/(?=[a-z]+)/" able get: "sf berkeley paso/slo/sb on 9/25"

my php code:

$re = "/([a-z]+)\\/(?=[a-z]+)/";       $subst = "$1 ";  $result = preg_replace($re, $subst, $str, 1); 

any appreciated!

you need use assertions.

preg_replace('~(?<=[a-z])/(?=[a-z])~i', ' ', $str); 

update:

your code works, need remove last 1 parameter, means replacing 1 time.

$re = "/([a-z]+)\\/(?=[a-z]+)/";       $subst = "$1 ";  $result = preg_replace($re, $subst, $str); 

and don't need add + after [a-z].

demo


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -