Advertisement
Guest User

Amer Kawar

a guest
Oct 31st, 2009
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.52 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Function to fix video objects embedding HTML code in Wordpress.
  5.  * This function uses DOM traversal to rebuild the <object> tag of the
  6.  * video files in an XHTML valid format.
  7.  *
  8.  * Article: HOW TO: Build a XHTML Valid Wordpress Blog with DISQUS Plugin
  9.  * URL: http://blog.thoughtpick.com/2009/10/how-to-build-xhtml-valid-wordpress.html
  10.  *
  11.  * @author: Amer Kawar
  12.  */
  13. function fixVideoObjects($content) {
  14.  
  15.   // input the regex pattern to match <object *</object> tags
  16.   $pattern = '/<object [a-z0-9\/\- \.=_#:,"\?\'><&;]*<\/object>/i';
  17.  
  18.   // use preg_match_all to fill $m array with all <object> fields found
  19.   $m = array();
  20.   preg_match_all($pattern, $content, $m);
  21.  
  22.   // for each match in $m loop and replace the object only if
  23.   // it has the embed tag
  24.   if ( isset($m[0]) && count($m[0]) > 0 ) {
  25.     for ($i = 0 ; $i < count($m[0]); $i++) {
  26.       if (isset($m[0][$i])) {
  27.         $doc = new DOMDocument();
  28.         $doc->loadHTML($m[0][$i]);
  29.         $o = $doc->getElementsByTagName('object');
  30.  
  31.         if ( !is_null($o->item(0)) ) {
  32.           $embed_tag = $o->item(0)->getElementsByTagName('embed');
  33.           if (!is_null($embed_tag->item(0))) {
  34.             $src = $embed_tag->item(0)->getAttribute('src');
  35.  
  36.             // get the w x h, if they are null from the <object> tag, get them from
  37.             // the <embed> tag
  38.             $w = $o->item(0)->getAttribute('width');
  39.             $h = $o->item(0)->getAttribute('height');
  40.             if (is_null($w)) $w = $embed_tag->item(0)->getAttribute('width');
  41.             if (is_null($h)) $h = $embed_tag->item(0)->getAttribute('height');
  42.  
  43.             $out = '<object type="application/x-shockwave-flash" '
  44.                    .'style="width:'.$w.'px; '
  45.                    .'height:'.$h.'px;" data="'.str_replace('&','&amp;',$src).'">';
  46.  
  47.             // find all param tags and re-echo them in XHTML 1.0 format
  48.             $params = $o->item(0)->getElementsByTagName('param');
  49.             foreach ($params as $p) {
  50.               if (!is_null($p->getAttribute('name')) && !is_null($p->getAttribute('value'))) {
  51.                 $n = $p->getAttribute('name');
  52.                 $v = $p->getAttribute('value');
  53.                 $out .= '<param name="'.$n.'" value="'.str_replace('&','&amp;',$v).'" />';
  54.               }
  55.             }
  56.  
  57.             $out .= '</object>';
  58.  
  59.             // replace object found by the re-written object
  60.             $content = str_replace($m[0][$i], $out, $content);
  61.           }
  62.         }
  63.       }
  64.     }
  65.   }
  66.  
  67.   return $content;
  68. }
  69.  
  70. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement