preg_replace第一个匹配模式包括斜杠

How to remove first pattern match when string content "/"

$a = "abc/def";
$b = "abc/def/ghi/abc/def";

$result = "/ghi/abc/def"; when replace with "abc/def" only look for the fist match


I try this but is not work.
$x  = preg_replace('/'.$a.'/', '', $b, 1);

You have to keep in mind that first argument of preg_replace is a RegEx pattern, so you can't pass just any string to it.

First you need to escape all regex characters with function preg_quote

Try:

$a = "abc/def";
$b = "abc/def/ghi/abc/def";

$pattern = preg_quote($a, '/'); // second argument allows us to escape delimiter chars, too

$x  = preg_replace("/$pattern/", '', $b, 1);