PHP正則匹配html標籤
一、前言
1、匹配字符串時用:.*? 或 [^\<]+\ 或 [^\<]+
2、preg_match匹配結果爲1個,preg_match_all匹配結果爲多個
3、如果有標籤結束,則要加符號\,例如<\/div>,如果只需要匹配標籤裏的屬性,就不需要匹配標籤的結束符號<\/標籤名>
4、preg_match_all匹配多個時,每一個()裏對應一個結果數組,默認匹配返回所有內容,preg_match_all返回的數組數量 = 1 + ()的個數
5、相關文章 正則表達式 - php正則匹配內容?https://www.php.cn/php-weizijiaocheng-285574.html
二、代碼
<?php
namespace app\index\controller;
class Ablog extends Base
{
public function test4(){
//獲取Html字符串
$html = $this->getHtml();
//打印標題
$this->searchTitle($html);
//打印內容
$this->searchContent($html);
}
public function searchContent($html){
// $pattern1 = '/<div class="info" id=".*?">[^\"]+<\/div>/';
$pattern1 = '/<div class="info" id=".*?">[^\<]+\<\/div>/';
// $pattern2 = '/<div class="info" id="(.*?)">([^\"]+)<\/div>/';
$pattern2 = '/<div class="info" id="(.*?)">([^\<]+)<\/div>/';
preg_match($pattern1, $html, $matches1One); //匹配單個
preg_match_all($pattern1, $html, $matches1More); //匹配多個
var_export($matches1One);
<<<EOF
matches1One:打印
array (
0 => '<div class="info" id="96">
回來的時候換了一條路。那裏進過廣場的公園。一如既往的有好多手機貼膜的小販還有隻有一個板凳面前一張紙賣電話卡的小販。
</div>',
)
EOF;
var_export($matches1More);
<<<EOF
matches1More:打印
array (
0 =>
array (
0 => '<div class="info" id="96">
回來的時候換了一條路。那裏進過廣場的公園。一如既往的有好多手機貼膜的小販還有隻有一個板凳面前一張紙賣電話卡的小販。
</div>',
1 => '<div class="info" id="97">
我看見石榴花開了,紅的黃的真的特別的好看。月季依然開得很嬌豔。
</div>',
),
)
EOF;
preg_match($pattern2, $html, $matches2One); //匹配所有
preg_match_all($pattern2, $html, $matches2More); //匹配所有
var_export($matches2One);
<<<EOF
matches2One:打印
array (
0 => '<div class="info" id="96">
回來的時候換了一條路。那裏進過廣場的公園。一如既往的有好多手機貼膜的小販還有隻有一個板凳面前一張紙賣電話卡的小販。
</div>',
1 => '96',
2 => '
回來的時候換了一條路。那裏進過廣場的公園。一如既往的有好多手機貼膜的小販還有隻有一個板凳面前一張紙賣電話卡的小販。
',
)
EOF;
var_export($matches2More);
<<<EOF
matches2More:打印
array (
0 =>
array (
0 => '<div class="info" id="96">
回來的時候換了一條路。那裏進過廣場的公園。一如既往的有好多手機貼膜的小販還有隻有一個板凳面前一張紙賣電話卡的小販。
</div>',
1 => '<div class="info" id="97">
我看見石榴花開了,紅的黃的真的特別的好看。月季依然開得很嬌豔。
</div>',
),
1 =>
array (
0 => '96',
1 => '97',
),
2 =>
array (
0 => '
回來的時候換了一條路。那裏進過廣場的公園。一如既往的有好多手機貼膜的小販還有隻有一個板凳面前一張紙賣電話卡的小販。
',
1 => '
我看見石榴花開了,紅的黃的真的特別的好看。月季依然開得很嬌豔。
',
),
)
EOF;
}
public function searchTitle($html){
$titlePattern1 = '/<h2>.*?<\/h2>/'; //匹配:(h2標籤 + 標籤裏的內容)
$titlePattern2 = '/<h2>(.*?)<\/h2>/';//匹配:(h2標籤 + 標籤裏的內容)和 (h2標籤裏的內容)
//titlePattern1
preg_match($titlePattern1, $html, $titleMatches1One); //匹配單個
preg_match_all($titlePattern1, $html, $titleMatches1More); //匹配多個
var_export($titleMatches1One);
<<<EOF
titleMatches1One:打印
array (
0 => '<h2>美好的一天</h2>',
)
EOF;
var_export($titleMatches1More);
<<<EOF
titleMatches1More:打印
array (
0 =>
array (
0 => '<h2>美好的一天</h2>',
),
)
EOF;
//titlePattern2
preg_match($titlePattern2, $html, $titleMatches2One); //匹配單個
preg_match_all($titlePattern2, $html, $titleMatches2More); //匹配多個
var_export($titleMatches2One);
<<<EOF
titleMatches2One:打印
array (
0 => '<h2>美好的一天</h2>',
1 => '美好的一天',
)
EOF;
var_export($titleMatches2More);
<<<EOF
titleMatches2More:打印
array (
0 =>
array (
0 => '<h2>美好的一天</h2>',
),
1 =>
array (
0 => '美好的一天',
),
)
EOF;
}
public function getHtml(){
$html = '<html>
<head>
<title>大家好</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<!-- 文章START -->
<div class="whole">
<!-- 標題START -->
<div class="head">
<h2>美好的一天</h2>
<div>
<span>御風劍士</span>
<span>2019-04-24</span>
</div>
</div>
<!-- 標題SEND -->
<!-- 內容START -->
<div class="content">
<a href="http://www.***.com">
<img src="http://images.***.com"/>
</a>
<div class="info" id="96">
回來的時候換了一條路。那裏進過廣場的公園。一如既往的有好多手機貼膜的小販還有隻有一個板凳面前一張紙賣電話卡的小販。
</div>
<div class="info" id="97">
我看見石榴花開了,紅的黃的真的特別的好看。月季依然開得很嬌豔。
</div>
</div>
<!-- 內容END -->
</div>
<!-- 文章END -->
</body>
</html>';
return $html;
}
}