Guest User

Untitled

a guest
Feb 17th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1.     //phrase image and youtube links
  2.     foreach ($template->query('//article/div/p[a and count(*)=1 and not(normalize-space(text()))]') as $node) {
  3.         $link = trim($node->nodeValue);
  4.         $replace = false;
  5.  
  6.         if (
  7.             preg_match('~(?:
  8.                 https?://(?:www\.)?youtu(?:be\.com/(?:embed/|watch\?(?:.+&)?v=)|\.be/)([a-z0-9_\-]+) # Parsing YouTube URLs.
  9.                 |
  10.                 (.+\.(?:png|jpe?g|gif)$) # Parsing image URLs.
  11.             )~ixA', $link, $matches)
  12.         ) {
  13.             if (strlen($matches[1])) { // We found a YouTube video.
  14.                 $replace = '<iframe width="480" height="360" src="http://www.youtube-nocookie.com/embed/' . $matches[1] . '?autoplay=0&amp;rel=0" frameborder="0" allowfullscreen="allowfullscreen">' . safeHTML($link) . '</iframe>';
  15.             } else if (strlen($matches[2])) { // We found an image link.
  16.                 $replace = '<a target="_blank" href="' . safeHTML($link) . '"><img style="max-width: 100%;" src="' . $matches[2] . '" alt="Click to view full size image." /></a>';
  17.             }
  18.         }
  19.  
  20.         if ($replace) {
  21.             $frag = $node->ownerDocument->createDocumentFragment();
  22.             if (!@$frag->appendXML($replace) || !@$node->parentNode->replaceChild($frag, $node)) {
  23.                 throw new Exception("Invalid HTML");
  24.             }
  25.         }
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment