-1

I want to get <a> element of html. I use xPath /html/body/nav/div/a but not correct. Someone know how I get the Example text ?

<nav class="navbar  navbar-expand-xl" id="menu">
    <div>
<a>
<i class="fas fa-user mr-2"></i>
        Example text
</a>
    </div>

juros22
  • 1
  • 2
  • Given your example [strip_tags()](https://www.php.net/manual/en/function.strip-tags.php) would suffice, perhaps with a [trim()](https://www.php.net/manual/en/function.trim.php) added. – KIKO Software Nov 13 '22 at 21:57
  • 4
    Does this answer your question? [How to parse HTML in PHP?](https://stackoverflow.com/questions/18349130/how-to-parse-html-in-php) – Uwe Nov 13 '22 at 23:20

2 Answers2

0

You can parse it via DOM or use XPath.

Here is an example of each.
Demo: https://3v4l.org/nvcYP

$html = <<<'_HTML'
<nav class="navbar navbar-expand-xl" id="menu">
    <div>
        <a><i class="fas fa-user mr-2"></i>Example text</a>
    </div>
</nav>
_HTML;

libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($html);

// parsing the elements
$nav = $doc->getElementById('menu');
$text = $nav->getElementsByTagName('a')->item(0)->nodeValue;
echo $text;

// using xpath
$xpath = new DOMXPath($doc);
$text = $xpath->query('//nav[@id="menu"]/div/a')->item(0)->nodeValue;
echo $text;
Markus Zeller
  • 8,516
  • 2
  • 29
  • 35
-3

Your code should look like this :

<i class="fas fa-user mr-2"> Example text </i>

Here is the parse coding:

<?php
  $doc = new DOMDocument();
  $doc->loadHTML("<html><body><h1>Parsing Html in PHP</h1></body></html>");
  echo $doc->saveHTML();
?>

   
  • 1
    I understand your need to say this, and that you cannot yet comment, but it is not an answer to the question. – KIKO Software Nov 13 '22 at 22:24
  • I am new to this site. I added the coding you requested. – Shaman Technology Nov 13 '22 at 22:54
  • Yes, I know you're new. Welcome here. I hope you have a nice time exploring this site. As to your code, what does it accomplish? – KIKO Software Nov 13 '22 at 22:58
  • Thank you! The above code will output the h1 heading. – Shaman Technology Nov 13 '22 at 23:07
  • Agreed, but what is the point of that? And how does this relate to the question above? – KIKO Software Nov 13 '22 at 23:09
  • You can change the loadHTML information to match any website. You can use this code to parse elements using php. All the HTML coding inside the php command will change based on the site you want to parse. – Shaman Technology Nov 13 '22 at 23:14
  • OK, I see your point. But the idea of this site is to give useful answers to the question asked. An answer usually also contains an explanation of the code used. Your answer would be acceptable if it contained something like a reference to [getElementsByTagName()](https://www.php.net/manual/en/domdocument.getelementsbytagname.php) to extract the wanted tag and then displayed that. I think that's what you're implying, isn't it? – KIKO Software Nov 13 '22 at 23:20
  • You are being very critical of the free time I gave to answer your question. I can use the code I gave you to complete the task you desire to be completed. You seem very knowledgeable is this some kind of test when someone comes to help you ….for your entertainment? You should be more grateful to people willing to help you. – Shaman Technology Nov 13 '22 at 23:32
  • 1
    You misunderstand the situation. This happens to all of us. Please check my name and the name of the person asking the question. I am not the one asking the question. I'm trying to help you to give a proper answer. – KIKO Software Nov 13 '22 at 23:45