X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Foembed.php;h=4d2b7185e9a43d2f516a46e62461b605f0150d97;hb=e00068334f414b42cd7a25c594a47efb86d5bb2f;hp=37923a877bf7193f44f862fa1920360fdbf66029;hpb=c26463b65b4b98f9aca77781fd74a7fe99f73814;p=friendica.git diff --git a/include/oembed.php b/include/oembed.php index 37923a877b..4d2b7185e9 100644 --- a/include/oembed.php +++ b/include/oembed.php @@ -79,19 +79,31 @@ function oe_get_inner_html( $node ) { * and replace it with [embed]url[/embed] */ function oembed_html2bbcode($text) { - $dom = DOMDocument::loadHTML($text); - $xpath = new DOMXPath($dom); - $attr = "oembed"; - - $xattr = oe_build_xpath("class","oembed"); - $entries = $xpath->query("//span[$xattr]"); - - $xattr = oe_build_xpath("rel","oembed"); - foreach($entries as $e) { - $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue; - if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[embed]"), $e); - } - return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) ); + // start parser only if 'oembed' is in text + if (strpos($text, "oembed")){ + + // convert non ascii chars to html entities + $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text)); + + // If it doesn't parse at all, just return the text. + $dom = @DOMDocument::loadHTML($html_text); + if(! $dom) + return $text; + $xpath = new DOMXPath($dom); + $attr = "oembed"; + + $xattr = oe_build_xpath("class","oembed"); + $entries = $xpath->query("//span[$xattr]"); + + $xattr = oe_build_xpath("rel","oembed"); + foreach($entries as $e) { + $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue; + if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[embed]"), $e); + } + return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) ); + } else { + return $text; + } } ?> \ No newline at end of file