<?php $html = <<<HTML <div> Some text <img src="https://example.com/foo?bar=food"> and a raw link http://example.com/number2 then <a href="https://example.com/the/third/url">original text</a> ... and <p>here's another HTTPS://www.example.net/booyah</p> and done </div> HTML; $dom = new DOMDocument(); $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); $regex = '#\bhttps?://[-\w.]+(?::\d+)?(?:/(?:[\w/_.-]*(?:\?\S+)?)?)?#ui'; foreach ($xpath->query('//*[not(self::a)]/text()') as $textNode) { $text = $textNode->nodeValue; foreach (preg_match_all($regex, $text, $m) ? $m[0] : [] as $url) { $a = $dom->createElement('a', htmlspecialchars($url)); $a->setAttribute('href', $url); $mbPosOfUrlInText = mb_strpos($text, $url); // regurgitate any leading text as newpreceding node // then replace remainder of text with new hyperlink $textNode->parentNode->replaceChild( $a, $textNode->splitText($mbPosOfUrlInText) ); // add any text after url as new text node after new hyperlink $textNode->parentNode->insertBefore( $dom->createTextNode( mb_substr($text, $mbPosOfUrlInText + mb_strlen($url)) ), $a->nextSibling ); } } echo $dom->saveHTML();
You have javascript disabled. You will not be able to edit any code.
Value for `_results` contains invalid data `array`