Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/questions/4231382/regular-expression-pattern-not-matching-anywhere-in-string/4234491#4234491">Reliable Regex for HTML are difficult</a>. Here is how to do it with <a href="http://de.php.net/manual/en/book.dom.php" rel="noreferrer">DOM</a>:</p> <pre><code>$dom = new DOMDocument; $dom-&gt;loadHTML($html); foreach ($dom-&gt;getElementsByTagName('a') as $node) { echo $dom-&gt;saveHtml($node), PHP_EOL; } </code></pre> <p>The above would find and output the <a href="https://stackoverflow.com/questions/5404941/php-domdocument-outerhtml-for-element/5404962#5404962">"outerHTML"</a> of all <code>A</code> elements in the <code>$html</code> string. </p> <p>To <strong>get</strong> all the text values of the node, you do</p> <pre><code>echo $node-&gt;nodeValue; </code></pre> <p>To <strong>check</strong> if the <code>href</code> attribute exists you can do</p> <pre><code>echo $node-&gt;hasAttribute( 'href' ); </code></pre> <p>To <strong>get</strong> the <code>href</code> attribute you'd do</p> <pre><code>echo $node-&gt;getAttribute( 'href' ); </code></pre> <p>To <strong>change</strong> the <code>href</code> attribute you'd do</p> <pre><code>$node-&gt;setAttribute('href', 'something else'); </code></pre> <p>To <strong>remove</strong> the <code>href</code> attribute you'd do</p> <pre><code>$node-&gt;removeAttribute('href'); </code></pre> <p>You can also query for the <code>href</code> attribute directly with <a href="http://schlitt.info/opensource/blog/0704_xpath.html" rel="noreferrer">XPath</a></p> <pre><code>$dom = new DOMDocument; $dom-&gt;loadHTML($html); $xpath = new DOMXPath($dom); $nodes = $xpath-&gt;query('//a/@href'); foreach($nodes as $href) { echo $href-&gt;nodeValue; // echo current attribute value $href-&gt;nodeValue = 'new value'; // set new attribute value $href-&gt;parentNode-&gt;removeAttribute('href'); // remove attribute } </code></pre> <p>Also see:</p> <ul> <li><a href="https://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662">Best methods to parse HTML</a></li> <li><a href="https://stackoverflow.com/questions/4979836/noob-question-about-domdocument-in-php/4983721#4983721">DOMDocument in php</a> </li> </ul> <p><em>On a sidenote: I am sure this is a duplicate and you can <a href="https://stackoverflow.com/search?q=find+all+href+attributes+dom+php">find the answer somewhere in here</a></em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload