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

java - How to pass parameters between Request-Scoped Controllers? -

string - what is the difference between "some" == "some\0" and strcmp("some","some\0") in c++? -

actionscript 3 - Flex: moving a point with rotation doesnt change the point XY? -