Amer Kawar
By: a guest | Oct 31st, 2009 | Syntax:
PHP | Size: 2.52 KB | Hits: 189 | Expires: Never
<?php
/**
* Function to fix video objects embedding HTML code in Wordpress.
* This function uses DOM traversal to rebuild the <object> tag of the
* video files in an XHTML valid format.
*
* Article: HOW TO: Build a XHTML Valid Wordpress Blog with DISQUS Plugin
* URL: http://blog.thoughtpick.com/2009/10/how-to-build-xhtml-valid-wordpress.html
*
* @author: Amer Kawar
*/
function fixVideoObjects($content) {
// input the regex pattern to match <object *</object> tags
$pattern = '/<object [a-z0-9\/\- \.=_#:,"\?\'><&;]*<\/object>/i';
// use preg_match_all to fill $m array with all <object> fields found
// for each match in $m loop and replace the object only if
// it has the embed tag
for ($i = 0
; $i < count($m[0
]); $i++) {
$doc = new DOMDocument();
$doc->loadHTML($m[0][$i]);
$o = $doc->getElementsByTagName('object');
$embed_tag = $o->item(0)->getElementsByTagName('embed');
if (!is_null($embed_tag->item(0
))) {
$src = $embed_tag->item(0)->getAttribute('src');
// get the w x h, if they are null from the <object> tag, get them from
// the <embed> tag
$w = $o->item(0)->getAttribute('width');
$h = $o->item(0)->getAttribute('height');
if (is_null($w)) $w = $embed_tag->item(0)->getAttribute('width');
if (is_null($h)) $h = $embed_tag->item(0)->getAttribute('height');
$out = '<object type="application/x-shockwave-flash" '
.'style="width:'.$w.'px; '
.'height:'.$h.'px;" data="'.str_replace('&','&',$src).'">';
// find all param tags and re-echo them in XHTML 1.0 format
$params = $o->item(0)->getElementsByTagName('param');
foreach ($params as $p) {
if (!is_null($p->getAttribute('name')) && !is_null($p->getAttribute('value'))) {
$n = $p->getAttribute('name');
$v = $p->getAttribute('value');
$out .= '<param name="'.$n.'" value="'.str_replace('&','&',$v).'" />';
}
}
$out .= '</object>';
// replace object found by the re-written object
}
}
}
}
}
return $content;
}
?>