From 73c32678ae44d33aea36696cf7db3f382476b7c1 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 16 Jan 2012 15:31:01 +0400 Subject: rewrite_urls implementation based on DOMDocument (refs #413) --- include/functions.php | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'include/functions.php') diff --git a/include/functions.php b/include/functions.php index 5ccbe8a58..e35d7c1c6 100644 --- a/include/functions.php +++ b/include/functions.php @@ -4849,7 +4849,7 @@ } - function rewrite_urls($line) { +/* function rewrite_urls($line) { global $url_regex; $urls = null; @@ -4858,6 +4858,48 @@ "\\1", $line); return $result; + } */ + + function rewrite_urls($html) { + libxml_use_internal_errors(true); + + $charset_hack = ' + + '; + + $doc = new DOMDocument(); + $doc->loadHTML($charset_hack . $html); + $xpath = new DOMXPath($doc); + + $entries = $xpath->query('//*/text()'); + + foreach ($entries as $entry) { + if (strstr($entry->wholeText, "://") !== false) { + $text = preg_replace("/((?\\1", $entry->wholeText); + + if ($text != $entry->wholeText) { + $cdoc = new DOMDocument(); + $cdoc->loadHTML($charset_hack . $text); + + + foreach ($cdoc->childNodes as $cnode) { + $cnode = $doc->importNode($cnode, true); + + if ($cnode) { + $entry->parentNode->insertBefore($cnode); + } + } + + $entry->parentNode->removeChild($entry); + + } + } + } + + $node = $doc->getElementsByTagName('body')->item(0); + + return $doc->saveXML($node); } function filter_to_sql($filter) { -- cgit v1.2.3