[英]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.