Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //phrase image and youtube links
- foreach ($template->query('//article/div/p[a and count(*)=1 and not(normalize-space(text()))]') as $node) {
- $link = trim($node->nodeValue);
- $replace = false;
- if (
- preg_match('~(?:
- https?://(?:www\.)?youtu(?:be\.com/(?:embed/|watch\?(?:.+&)?v=)|\.be/)([a-z0-9_\-]+) # Parsing YouTube URLs.
- |
- (.+\.(?:png|jpe?g|gif|webp)$) # Parsing image URLs.
- |
- (.+\.(?:webm|mp4|mov|3gp|avi)$) # Parsing video URLs.
- )~ixA', $link, $matches)
- ) {
- if (strlen($matches[1])) { // We found a YouTube video.
- $replace = '<iframe width="100%" height="360" src="http://www.youtube-nocookie.com/embed/' . $matches[1] . '?autoplay=0&rel=0" frameborder="0" allowfullscreen="allowfullscreen">' . safeHTML($link) . '</iframe>';
- } else if (strlen($matches[2])) { // We found an image link.
- $replace = '<a target="_blank" href="' . safeHTML($link) . '"><img style="max-width: 100%;" src="' . $matches[2] . '" alt="Click to view full size image." /></a>';
- } else if (strlen($matches[3])) { // We found an video link.
- $replace = '<video style="max-width: 100%;" src="' . $matches[3] . '" controls />';
- }
- }
- if ($replace) {
- $frag = $node->ownerDocument->createDocumentFragment();
- if (!@$frag->appendXML($replace) || !@$node->parentNode->replaceChild($frag, $node)) {
- throw new Exception("Invalid HTML");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment