将变量连接到正则表达式php

时间:2015-07-17 03:24:37

标签: php regex

我有这些数组(数组和数组2)

$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

1 个答案:

答案 0 :(得分:2)

要匹配//之后和第一个点之前的所有内容,您需要使用\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);