[英]Concatenating variable into a regex php
我有这些数组(array和array2)
$urls = array("http://piggington.com/pb_cash_flow_positive")
我有这个正则表达式
(preg_match("/\/{2}.*?\./", $array[$i], $matches))
它检查第二斜杠之后和第一点之前的所有内容。 所以它将发现
/piggington.
现在想在下面的正则表达式内连接一个变量,因此它将搜索特定的字符串。
我试过了:
$matches_imploded = implode($matches);
$matches_imploded = preg_quote($matches_imploded, '/');
$match_with_other_array = preg_grep("/\/{2}".$matches_imploded."\./", $array2);
但这没有找到任何匹配。.我在做什么错? 它应该在array2内部查看并与$ matches_imploded进行正匹配
between second slash and first dot we found $matches_imploded
要匹配//
之后和第一个点之前的所有内容,您需要使用\\K
或正向后看。
preg_match("~/{2}\K[^.]*(?=.)~", $array[$i], $matches)
$matches_imploded = implode($matches);
$matches_imploded = preg_quote($matches_imploded, '/');
$match_with_other_array = preg_grep("/\/{2}".$matches_imploded."\./", $array2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.