-1

everybody,

can someone please help with something.

I would like to search a list by name. As an example: Naruto

However, there are not only Naruto, but also "Naruto Shippuden" and "Boruto: Naruto Next Generations".

Now I want the other two to be displayed when the user searches for "Naruto".

<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>

So far, I've tried that.

$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);
  • So, did you try anything? Can you show us your attempts? – u_mulder Sep 17 '18 at 14:36
  • Cna you show your relevant code? – Sfili_81 Sep 17 '18 at 14:37
  • Where do your list come from? A database? hardcoded? In other words, to what should apply your search? – Cid Sep 17 '18 at 14:37
  • It's in an HTML file. I have edited my post with my attempt. –  Sep 17 '18 at 14:58
  • Are you trying to match HTML with a regex? [This is slightly relevant.](https://stackoverflow.com/a/1732454/1531971) And we should never let a chance go by to post this answer. EVER. –  Sep 17 '18 at 15:28
  • I don't understand the Post. I need a practical example to see how this is possible. I thought Regex was the best way. How else can I search by name and display similar names? –  Sep 17 '18 at 15:33
  • Because HTML isn't regular. Attributes in tags can be in any order. There can be human errors (non closed tags in example). Tags can be nested etc... – Cid Sep 18 '18 at 06:41

1 Answers1

1

I strongly advise you to use PHP Simple HTML DOM Parser for your purpose, it's easy to use.

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>');

Outputs as 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>
Cid
  • 14,968
  • 4
  • 30
  • 45