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]
.
Comments
Post a Comment