Php preg_match_所有项目的数组,然后针对每个匹配
我正在编辑一些Interspire电子邮件代码。目前,该程序在发送电子邮件之前会浏览电子邮件的HTML,并查找“a href”代码以替换链接。我希望它也通过并获得formaction=并替换它们中当前没有的URL。我想我可以使用下面这篇文章中的正则表达式: 但是我在思考如何处理阵列时遇到了一些困难。仅执行“a href=”的当前代码如下:Php preg_match_所有项目的数组,然后针对每个匹配,php,regex,arrays,preg-match-all,Php,Regex,Arrays,Preg Match All,我正在编辑一些Interspire电子邮件代码。目前,该程序在发送电子邮件之前会浏览电子邮件的HTML,并查找“a href”代码以替换链接。我希望它也通过并获得formaction=并替换它们中当前没有的URL。我想我可以使用下面这篇文章中的正则表达式: 但是我在思考如何处理阵列时遇到了一些困难。仅执行“a href=”的当前代码如下: preg_match_all('%<a.+(href\s*=\s*(["\']?[^>"\']+?))\s*.+>%isU', $t
preg_match_all('%<a.+(href\s*=\s*(["\']?[^>"\']+?))\s*.+>%isU', $this->body['h'], $matches);
$links_to_replace = $matches[2];
$link_locations = $matches[1];
arsort($link_locations);
reset($links_to_replace);
reset($link_locations);
foreach ($link_locations as $tlinkid => $url) {
// so we know whether we need to put quotes around the replaced url or not.
$singles = false;
$doubles = false;
// make sure the quotes are matched up.
// ie there is either 2 singles or 2 doubles.
$quote_check = substr_count($url, "'");
if (($quote_check % 2) != 0) {
...
现在还不成立,是吗?有可能做到我想的吗?或者我是否需要编写另一个函数来处理与“a href”分开的“forms action=”a建议:
$pattern = <<<'LOD'
~
(?| # branch reset feature: allows to have the same named
# capturing group in an alternation. ("type" here)
<a\s # the link case
(?> # atomic group: possible content before the "href" attribute
[^h>]++ # all that is not a "h" or the end of the tag ">"
|
\Bh++ # all "h" not preceded by a word boundary
|
h(?!ref\s*+=) # all "h" not followed by "ref=" or "ref ="
)*+ # repeat the atomic group zero or more times.
(?<type> href )
| #### OR ####
<form\s # the form case
(?> # possible content before the "action" attribute. (same principle)
[^a>]++
|
\Ba++
|
a(?!ction\s*+=)
)*+
(?<type> action )
)
\s*+ = \s*+ # optional spaces before and after the "=" sign
\K # resets all on the left from match result
(?<quote> ["']?+ )
(?<url> [^\s"'>]*+ )
\g{quote} # backreference to the "quote" named capture (", ', empty)
~xi
LOD;
看起来您试图在preg_match_all中传递多个参数。为什么不使用preg_replace来替换action=URL或DOM格式?如果我假设正确,您希望替换数组元素中的URL,最好使用preg_replace_callback。如果您希望执行替换,为什么不使用preg_replace或preg_replace_callback?我对PHP不是很精通,所以不知道最好的方法或唯一的方法。所以,如果我想添加一个函数,通常我只需要离开已经存在的东西,并尝试添加到它。你是说preg_match_一切都不管用?我假设他们使用这个函数是有原因的,这取决于preg_replace,就像我说的,如果你想找到所有form action=URL,然后从数组元素中替换这些URL,那么最好使用preg_replace_回调函数来执行。如果不是这样,那么请更清楚地说明你到底想做什么=
$links_to_replace = $matches[2];
$link_locations = $matches[1];
$pattern = <<<'LOD'
~
(?| # branch reset feature: allows to have the same named
# capturing group in an alternation. ("type" here)
<a\s # the link case
(?> # atomic group: possible content before the "href" attribute
[^h>]++ # all that is not a "h" or the end of the tag ">"
|
\Bh++ # all "h" not preceded by a word boundary
|
h(?!ref\s*+=) # all "h" not followed by "ref=" or "ref ="
)*+ # repeat the atomic group zero or more times.
(?<type> href )
| #### OR ####
<form\s # the form case
(?> # possible content before the "action" attribute. (same principle)
[^a>]++
|
\Ba++
|
a(?!ction\s*+=)
)*+
(?<type> action )
)
\s*+ = \s*+ # optional spaces before and after the "=" sign
\K # resets all on the left from match result
(?<quote> ["']?+ )
(?<url> [^\s"'>]*+ )
\g{quote} # backreference to the "quote" named capture (", ', empty)
~xi
LOD;
$html = preg_replace_callback($pattern,
function ($m) {
$url = $m['url'];
$type = lowercase($m['type']);
$quote = $m['quote'];
// make what you want with the url, type and quotes
return $quote . $url . $quote;
}, $html);