大家
有人可以帮忙吗?
我想按名称搜索列表。例如:火影忍者
但是,不仅有火影忍者,还有“火影忍者疾风传”和“火影忍者:火影忍者下一代”。
现在,我希望在用户搜索“火影忍者”时显示其他两个。
<li><a href="#Naruto" title="Naruto">Naruto</a></li>
<li><a href="#Naruto-Shippuden" title="Naruto Shippuden">Naruto Shippuden</a></li>
<li><a href="#Boruto-Naruto-Next-Generations" title="Boruto: Naruto Next Generations">Boruto: Naruto Next Generations</a></li>
到目前为止,我已经尝试过了。
$re = '/<li><a href=".*" title="(Naruto|(.*?))">(Naruto|(.*?))<\/a><\/li>/m';
$str = '<ul>
<li><a href="serie/Boruto-Naruto-Next-Generations" title="Boruto: Naruto Next Generations">Boruto: Naruto Next Generations</a></li>
<li><a href="serie/Masou-Gakuen-HxH" title="Masou Gakuen HxH">Masou Gakuen HxH</a></li>
<li><a href="serie/Matantei-Loki-Ragnarok" title="Matantei Loki Ragnarok">Matantei Loki Ragnarok</a></li>
<li><a href="serie/Mawaru-Penguindrum" title="Mawaru Penguindrum">Mawaru Penguindrum</a></li>
<li><a href="serie/Nanbaka" title="Nanbaka">Nanbaka</a></li>
<li><a href="serie/Narutaru" title="Narutaru">Narutaru</a></li>
<li><a href="serie/Naruto" title="Naruto">Naruto</a></li>
<li><a href="serie/Naruto-Shippuden" title="Naruto Shippuden">Naruto Shippuden</a></li>
<li><a href="serie/Natsu-iro-Kiseki" title="Natsu-iro Kiseki">Natsu-iro Kiseki</a></li>
<li><a href="serie/Natsume-Yuujinchou" title="Natsume Yuujinchou">Natsume Yuujinchou</a></li>
</ul>';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
答案 0 :(得分:2)
我强烈建议您使用PHP Simple HTML DOM Parser来达到目的,这很容易使用。
include_once("simple_html_dom.php");
$str = '<ul>
<li><a href="serie/Boruto-Naruto-Next-Generations" title="Boruto: Naruto Next Generations">Boruto: Naruto Next Generations</a></li>
<li><a href="serie/Masou-Gakuen-HxH" title="Masou Gakuen HxH">Masou Gakuen HxH</a></li>
<li><a href="serie/Matantei-Loki-Ragnarok" title="Matantei Loki Ragnarok">Matantei Loki Ragnarok</a></li>
<li><a href="serie/Mawaru-Penguindrum" title="Mawaru Penguindrum">Mawaru Penguindrum</a></li>
<li><a href="serie/Nanbaka" title="Nanbaka">Nanbaka</a></li>
<li><a href="serie/Narutaru" title="Narutaru">Narutaru</a></li>
<li><a href="serie/Naruto" title="Naruto">Naruto</a></li>
<li><a href="serie/Naruto-Shippuden" title="Naruto Shippuden">Naruto Shippuden</a></li>
<li><a href="serie/Natsu-iro-Kiseki" title="Natsu-iro Kiseki">Natsu-iro Kiseki</a></li>
<li><a href="serie/Natsume-Yuujinchou" title="Natsume Yuujinchou">Natsume Yuujinchou</a></li>
</ul>';
$htmlStr = str_get_html($str);
echo ('<ul>');
foreach ($htmlStr->find('a[title*="naruto"]') as $link) //find all link having the title attribute containing naruto
echo ('<li>' . $link . '</li><br />');
echo ('</ul>');
以HTML格式输出:
<ul>
<li><a href="serie/Boruto-Naruto-Next-Generations" title="Boruto: Naruto Next Generations">Boruto: Naruto Next Generations</a></li><br />
<li><a href="serie/Naruto" title="Naruto">Naruto</a></li><br />
<li><a href="serie/Naruto-Shippuden" title="Naruto Shippuden">Naruto Shippuden</a></li><br />
</ul>