PHP 正则表达式从字符串中排除两个字符串
PHP RegEx Excluding TWO strings from string
在搜索了各种其他问题以尝试自己解决这个问题之后,我已经走到这一步了,但我不知道如何添加一个额外的字符串。
// $url will actually be $_SERVER['REQUEST_URI']
$url = "/folder123/?id=foo"; // it could be "index.php?id=foo"
preg_replace_callback('#^/folder123/#', function($match) {
return $match[1];
}, $url);
预期结果: ?id=foo
用我现在的代码,我会得到预期的结果;但是我不知道如何检查 index.php。我的意图是让字符串排除这两个部分。
要匹配它们,您可以使用 alternation 后跟 \K
来重置报告匹配的起点。
^(?:/folder123/|index\.php)\K.*$
preg_replace_callback('#^(?:/folder123/|index\.php)\K.*$#', function($match) { return $match[0]; }, $url);
希望对您有所帮助:
$url = "index.php?id=foo22"; // it could be "index.php?id=foo"
preg_replace_callback('/(\?|\&).*/', function($match) {
var_dump($match[0]) ;
}, $url);
在搜索了各种其他问题以尝试自己解决这个问题之后,我已经走到这一步了,但我不知道如何添加一个额外的字符串。
// $url will actually be $_SERVER['REQUEST_URI']
$url = "/folder123/?id=foo"; // it could be "index.php?id=foo"
preg_replace_callback('#^/folder123/#', function($match) {
return $match[1];
}, $url);
预期结果: ?id=foo
用我现在的代码,我会得到预期的结果;但是我不知道如何检查 index.php。我的意图是让字符串排除这两个部分。
要匹配它们,您可以使用 alternation 后跟 \K
来重置报告匹配的起点。
^(?:/folder123/|index\.php)\K.*$
preg_replace_callback('#^(?:/folder123/|index\.php)\K.*$#', function($match) { return $match[0]; }, $url);
希望对您有所帮助:
$url = "index.php?id=foo22"; // it could be "index.php?id=foo"
preg_replace_callback('/(\?|\&).*/', function($match) {
var_dump($match[0]) ;
}, $url);