我希望替換hn不包含類屬性的標簽。這個想法是匹配任何后面的內容,hn除了一個包含 并以結尾的字串>
這是我的第一次嘗試:
<?php
$content = <<<HTML
<h1 style="color:black">test1</h1>
<H2 >test2</H2>
<h5 >test</h5>
<h5 >test test</h5>
HTML;
$content = preg_replace('#<h([1-6])((?!class).)*?>(.*?)<\/h[1-6]>#si', '<p ${2}>${3}</p>', $content);
echo ($content);
結果是:
<p class="heading-1" ">test1</p>
<H2 class="green">test2</H2>
<h5 class="red">test</h5>
<h5 class="">test test</h5>
它應該是:
<p class="heading-1" style="color:black">test1</p>
<H2 class="green">test2</H2>
<h5 class="red">test</h5>
<h5 class="">test test</h5>
知道為什么 $2 映射到"值而不是style="color:black"
uj5u.com熱心網友回復:
您的捕獲組必須添加到稍微不同的位置。
替換((?!class).)*?為((?:(?!class).)*?)。
采用
'#<h([1-6])\s*((?:(?!class).)*?)>(.*?)</h[1-6]>#si'
請參閱正則運算式證明。
解釋
--------------------------------------------------------------------------------
<h '<h'
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[1-6] any character of: '1' to '6'
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the least amount
possible)):
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
class 'class'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
)*? end of grouping
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
> '>'
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
) end of \3
--------------------------------------------------------------------------------
</h '</h'
--------------------------------------------------------------------------------
[1-6] any character of: '1' to '6'
--------------------------------------------------------------------------------
> '>'
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/444972.html
