[英]Regular Expression to match break line
我正在尋找刪除htmlA的正則表達式。 邊界從
<{if $ data.nowPage> 1}>到/ div> 1 breakLine <{/ if}>
HTMLA
<{if $data.nowPage>1}>
<div class="pageArrow pageFirst"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=1"><span class="icon arrow-first"></span></a></div>
<div class="pageArrow pagePrev"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=<{$data.nowPage-1}>"><span class="icon arrow-left"></span></a></div>
<{/if}>
我努力了
$fullHtml = readFileData($file);
preg_match("/(<{if \\\$data\.nowPage>1}>(.*?)<\/div>)(\s)(.+?)<{\/if}>/si",$fullHtml,$htmlA);
//$fullHtml = preg_replace(...,'',$fullHtml);
但是它停止得太早了,為什么print_r($ htmlA)顯示5個鍵?
Array
(
[0] => <{if $data.nowPage>1}>
<div class="pageArrow pageFirst"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=1"><span class="icon arrow-first"></span></a></div>
<div class="pageArrow pagePrev"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}>
[1] => <{if $data.nowPage>1}>
<div class="pageArrow pageFirst"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=1"><span class="icon arrow-first"></span></a></div>
[2] =>
<div class="pageArrow pageFirst"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=1"><span class="icon arrow-first"></span></a>
[3] =>
[4] =>
<div class="pageArrow pagePrev"><a href="?<{if !empty($data.years)}>y=<{$data.years}>
)
fullHtml。 第3到第6行。
<div class="pageContainer">
<div class="pageBox">
<{if $data.nowPage>1}>
<div class="pageArrow pageFirst"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=1"><span class="icon arrow-first"></span></a></div>
<div class="pageArrow pagePrev"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=<{$data.nowPage-1}>"><span class="icon arrow-left"></span></a></div>
<{/if}>
<{for $i=$data.nowPage-$data.pageFit to $data.totalPage}>
<{if $data.nowPage == ($i)}>
<div class="pageBtn active"><a href="javascript:void(0);"><{$i}></a></div>
<{else if $i>($data.nowPage+$data.pageFit)}>
<div class="pageMore"><span class="icon moreIcon"></span></div>
<{break}>
<{else if $i>0}>
<{if $i==$data.nowPage-$data.pageFit && $i>1}>
<div class="pageMore"><span class="icon moreIcon"></span></div>
<{/if}>
<div class="pageBtn"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=<{$i}>"><{$i}></a></div>
<{else}>
<{/if}>
<{/for}>
<{if $data.nowPage<$data.totalPage}>
<div class="pageArrow pageNext"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=<{$data.nowPage+1}>"><span class="icon arrow-right"></span></a></div>
<div class="pageArrow pageLast"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=<{$data.totalPage}>"><span class="icon arrow-last"></span></a></div>
<{/if}>
</div>
</div>
謝謝。
給定您的示例輸入,我為您的任務寫了一個模式。
格式: ~<{if \\$data\\.nowPage>1}>(?:\\s+<div.*?</div>)+\\s+<{/if}>~
代碼:( 演示 )
echo preg_replace('~<{if \$data\.nowPage>1}>(?:\s+<div.*?</div>)+\s+<{/if}>~','',$html);
if
線 <div>
行 if
行 這是您的RegEx,但是您忘記向其中添加模式,因此,一旦找到匹配項,它就會停止:
(<{if \\\$data\.nowPage>1}>(.*?)<\/div>)(\s)(.+?)<\/div>\s*<{\/if}>
^^^^^^^^^^
\\s
表示任何類型的空格 為什么
print_r($htmlA)
顯示5個鍵?
preg_match
函數在整體匹配時返回數組中的第一個值,而其他索引則包含捕獲組捕獲的值。 您的正則表達式中有4個捕獲組,因此它在返回數組中添加了4個值,這意味着總共有5個鍵。
正則表達式:
~<{if \$data\.nowPage>1}>.*?</div>\s*<{/if}>~si
PHP代碼:
preg_match('~<{if \$data\.nowPage>1}>.*?</div>\s*<{/if}>~si', $str, $htmlA);
您可以嘗試一下嗎。
const string = `<{if $data.nowPage>1}> <div class="pageArrow pageFirst"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=1"><span class="icon arrow-first"></span></a></div> <div class="pageArrow pagePrev"><a href="?<{if !empty($data.years)}>y=<{$data.years}><{/if}><{if !empty($data.month)}>&m=<{$data.month}><{/if}>&p=<{$data.nowPage-1}>"><span class="icon arrow-left"></span></a></div> <{/if}>`; console.log(string.match(/^<{if \\$data.nowPage>1}>\\n<div.*\\n*<div.*\\n*<\\/div>\\n<{\\/if}>/));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.