Heim >Backend-Entwicklung >PHP-Tutorial >php中正则无法匹配内容
<code><?php $output = ' <div class=obssin> <div class="substatus">看过</div> <ul> <li class="aob"> <a href="https://movie.douban.com/subject/3338862/" title="终结者:创世纪 终结者:创世纪 / 终结者5" target="_blank"> <img src="https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p2262649521.jpg" class="climg" alt="终结者:创世纪"> </a> </li> <li class="aob"> <a href="https://movie.douban.com/subject/1291844/" title="终结者2:审判日 终结者2:审判日 / 终结者2" target="_blank"> <img src="https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p1910909085.jpg" class="climg" alt="终结者2:审判日"> </a> </li> <li class="aob"> <a href="https://movie.douban.com/subject/1300656/" title="终结者 终结者 / 魔鬼终结者" target="_blank"> <img src="https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p511430453.jpg" class="climg" alt="终结者"> </a> </li> <li class="aob"> <a href="https://movie.douban.com/subject/24404677/" title="超体 超体 / LUCY:绝路煞姬(港)" target="_blank"> <img src="https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p2201909284.jpg" class="climg" alt="超体"> </a> </li> <li class="aob"> <a href="https://movie.douban.com/subject/25827963/" title="西游记之孙悟空三打白骨精 西游记之大闹天宫续集" target="_blank"> <img src="https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p2315805100.jpg" class="climg" alt="西游记之孙悟空三打白骨精"> </a> </li> </ul> <div class="clear"></div> '; $output=preg_replace("/[\t\n\r]+/","",$output); $partern='/<div class="obssin"> <div class="substatus">看过<ul><li class="aob"><a href="(%5B%5E<>%5D+)" title="([^<>]+)" target="_blank"><img src="(%5B%5E<>%5D+)" class="climg" alt="([^<>]+)">/'; preg_match_all($partern,$output,$result); var_dump($result); ?> <p>这样为什么匹配不到内容?</p> <h2>回复内容:</h2> <pre class="brush:php;toolbar:false"><code><?php $output = ' <div class=obssin> <div class="substatus">看过</div> <ul> <li class="aob"> <a href="https://movie.douban.com/subject/3338862/" title="终结者:创世纪 终结者:创世纪 / 终结者5" target="_blank"> <img src="https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p2262649521.jpg" class="climg" alt="终结者:创世纪"> </a> </li> <li class="aob"> <a href="https://movie.douban.com/subject/1291844/" title="终结者2:审判日 终结者2:审判日 / 终结者2" target="_blank"> <img src="https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p1910909085.jpg" class="climg" alt="终结者2:审判日"> </a> </li> <li class="aob"> <a href="https://movie.douban.com/subject/1300656/" title="终结者 终结者 / 魔鬼终结者" target="_blank"> <img src="https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p511430453.jpg" class="climg" alt="终结者"> </a> </li> <li class="aob"> <a href="https://movie.douban.com/subject/24404677/" title="超体 超体 / LUCY:绝路煞姬(港)" target="_blank"> <img src="https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p2201909284.jpg" class="climg" alt="超体"> </a> </li> <li class="aob"> <a href="https://movie.douban.com/subject/25827963/" title="西游记之孙悟空三打白骨精 西游记之大闹天宫续集" target="_blank"> <img src="https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p2315805100.jpg" class="climg" alt="西游记之孙悟空三打白骨精"> </a> </li> </ul> <div class="clear"></div></code>
改为:$partern='/<div class="obssin"><div class="substatus">看过<ul><li class="aob"><a href="(%5B%5E<>%5D+)" title="([^<>]+)" target="_blank"><img src="(%5B%5E<>%5D+)" class="climg" alt="([^<>]+)">/';
<p>PS:不建议这样用正则</p>
</a></li></ul>
</div></div>