简体   繁体   English

将变量连接到regex php中

[英]Concatenating variable into a regex php

I have these arrays (array and array2) 我有这些数组(array和array2)

$urls = array("http://piggington.com/pb_cash_flow_positive")

I have this regular expression 我有这个正则表达式

(preg_match("/\/{2}.*?\./", $array[$i], $matches))

It checks for everything that comes after 2nd slash and before 1st dot. 它检查第二斜杠之后和第一点之前的所有内容。 So it will find 所以它将发现

/piggington.

Now want to concatenate a variable inside the following regular expression, so it will search for a specific string. 现在想在下面的正则表达式内连接一个变量,因此它将搜索特定的字符串。

I tried: 我试过了:

$matches_imploded = implode($matches);
$matches_imploded = preg_quote($matches_imploded, '/');
$match_with_other_array = preg_grep("/\/{2}".$matches_imploded."\./", $array2);

But it's not finding any matches.. What am I doing wrong? 但这没有找到任何匹配。.我在做什么错? It should be looking inside array2 and making a positive match with $matches_imploded 它应该在array2内部查看并与$ matches_imploded进行正匹配

between second slash and first dot we found $matches_imploded

To match everything which comes after // and before the first dot, you need to use \\K or positive lookbehind. 要匹配//之后和第一个点之前的所有内容,您需要使用\\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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM