Php preg_replace_回调,删除撇号
我有下面的正则表达式回调,我遇到的问题是,在最终结果中,Php preg_replace_回调,删除撇号,php,regex,preg-replace-callback,Php,Regex,Preg Replace Callback,我有下面的正则表达式回调,我遇到的问题是,在最终结果中,“被删除。我能做些什么来防止这种情况发生 <?php $eval = "'$server.REQUEST_URI' == '/user/photos'"; $result = preg_replace_callback('/\'(.+)\'/isU', function($matches){ $match = $matches[1]; $find = Object69::find($match); retur
“
被删除。我能做些什么来防止这种情况发生
<?php
$eval = "'$server.REQUEST_URI' == '/user/photos'";
$result = preg_replace_callback('/\'(.+)\'/isU', function($matches){
$match = $matches[1];
$find = Object69::find($match);
return !empty($find) ? $find : $match;
}, $eval);
var_dump($result);
这就是我所期待的:
string(29) "'/user/photos' == '/user/photos'"
下面是它找到的数组$matches
:
array(2) {
[0]=>
string(21) "'$server.REQUEST_URI'"
[1]=>
string(19) "$server.REQUEST_URI"
}
array(2) {
[0]=>
string(14) "'/user/photos'"
[1]=>
string(12) "/user/photos"
}
Object69::find(
做什么?可能把引号放在捕获组中?Object69::find()
,在$\u服务器
数组中搜索
后的值并返回它。如果找不到该值,则返回一个空值'
或null
。为什么不在正则表达式中执行所有操作?@GetOffMyLawn我认为奇怪的对象69::find()
。您需要选择在哪里添加这些引号:在find()
方法中或在return语句中。另一种方法是在您的正则表达式中添加一些魔力:“/(?@HamZa,正则表达式正是我想要的!
array(2) {
[0]=>
string(21) "'$server.REQUEST_URI'"
[1]=>
string(19) "$server.REQUEST_URI"
}
array(2) {
[0]=>
string(14) "'/user/photos'"
[1]=>
string(12) "/user/photos"
}