Home >Backend Development >PHP Tutorial >正则小问题
$str = '
你要得到的事内容A和内容B呢,还是 id="test_0"的text和 id="test_1"的内容呢?
内容A 与 内容B
跟ID无关。不是JS。是PHP取数据。
$subject = '<div class="superStar" id="test_0"> you dead! <div class="show">hello</div> <span>james</span> </div> <div class="superStar" id="test_1"> shit! <div class="contne">hi</div> <ul><li>list</li></ul> </div>';$pattern = '/这里正则如何写/';preg_match($pattern, $subject, $matches);print_r($matches);$pattern_1 = '/这里正则如何写/';preg_match($pattern_1, $subject, $matches);print_r($matches);分别取到结果如下:数据一:you dead!<div class="show">hello</div><span>james</span>数据二:shit!<div class="contne">hi</div><ul><li>list</li></ul>
$str = '
默默收藏
preg_match_all('/
preg_match_all('/
array(3) { [0]=> array(2) { [0]=> string(86) "<div class="superStar" id="test_0"> you dead! <div class="show">hello</div>" [1]=> string(80) "<div class="superStar" id="test_1"> shit! <div class="contne">hi</div>" } [1]=> array(2) { [0]=> string(12) " id="test_0"" [1]=> string(12) " id="test_1"" } [2]=> array(2) { [0]=> string(45) " you dead! <div class="show">hello" [1]=> string(39) " shit! <div class="contne">hi" }}
少了〈/div〉
preg_match_all('/<div class=\"superStar\"(.*?)>(\s*(?:<div.*<\/div>\s*)*)<\/div>/us',$subject,$match);echo $match[0][0];echo '<hr />';echo $match[0][1];
<?php$subject = '<div class="superStar" id="test_0"> you dead! <div class="show">hello</div> <span>james</span> </div> <div class="superStar" id="test_1"> shit! <div class="contne">hi</div> <ul><li>list</li></ul> </div>';preg_match_all('/<div[^>]+>(([^<]*|<(?!div)|(?R))+)<\/div>/us',$subject,$match);echo $match[0][0];echo '<hr />';echo $match[0][1];
<?php$subject = '<div class="superStar" id="test_0"> you dead! <div class="show">hello</div> <span>james</span> </div> <div class="superStar" id="test_1"> shit! <div class="contne">hi</div> <ul><li>list</li></ul> </div>';preg_match_all('/<div[^>]+>(([^<]*|<(?!div)|(?R))+)<\/div>/us',$subject,$match);echo $match[0][0];echo '<hr />';echo $match[0][1];膜拜大神!