php - preg_replace first match pattern include slash -



php - preg_replace first match pattern include slash -

how remove first pattern match when string content "/"

$a = "abc/def"; $b = "abc/def/ghi/abc/def"; $result = "/ghi/abc/def"; when replace "abc/def" fist match seek not work. $x = preg_replace('/'.$a.'/', '', $b, 1);

you have maintain in mind first argument of preg_replace regex pattern, can't pass string it.

first need escape regex characters function preg_quote

try:

$a = "abc/def"; $b = "abc/def/ghi/abc/def"; $pattern = preg_quote($a, '/'); // sec argument allows escape delimiter chars, $x = preg_replace("/$pattern/", '', $b, 1);

php regex

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -