Reputation: 44383
hey guys,
a php variable $html
holds the following object...
<object width="562" height="200">
<param name="movie" value="http://www.youtube.com/v/rBa5qp9sUOY?version=3">
<param name="allowFullScreen" value="true">
<param name="allowscriptaccess" value="always">
<embed src="http://www.youtube.com/v/rBa5qp9sUOY?version=3"
type="application/x-shockwave-flash"
width="562"
height="200"
allowscriptaccess="always"
allowfullscreen="true">
</object>
any idea how I could filter the width and height both of the object- and embed-tag to have 100% values?
<object width="100%" height="100%">
...
<embed src="http://www.youtube.com/v/rBa5qp9sUOY?version=3"
type="application/x-shockwave-flash"
width="100%"
height="100%"
allowscriptaccess="always"
allowfullscreen="true">
</object>`
Thanks for your help!
update:
$chunk = $dom->getElementsByTagName('body')->item(0);
$objectHtml = '';
foreach($chunk->childNodes as $node) {
$objectHtml .= $node->saveXML();
}
return $objectHtml;
says: Call to undefined method DOMElement::saveXML()
whereas...
$dom = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveXML()));
works fine! however I get weird
inserted in my sourcecode when using this..
like this:
<!--?xml version="1.0" standalone="yes"?-->
<br>
<br>
<object width="95%" height="75%"><param name="movie" value="http://www.youtube.com/v/rBa5qp9sUOY?version=3"><param name="allowFullScreen" value="true"><param name="allowscriptaccess" value="always"><embed src="http://www.youtube.com/v/rBa5qp9sUOY?version=3" type="application/x-shockwave-flash" width="95%" height="75%" allowscriptaccess="always" allowfullscreen="true"></object>
any idea what I'm doing wrong here?
Upvotes: 0
Views: 3483
Reputation: 21378
preg_replace('/(width|height)(=)"([\d]+)"/', '${1}${2}"100%"', $html);
That should do the trick :)
Upvotes: 0
Reputation: 490461
It is often better to use a HTML parser instead of a regex when modifying HTML.
$dom = new DOMDocument;
$dom->loadHTML($str);
$object = $dom->getElementsByTagName('object')->item(0);
$object->setAttribute('width', '100%');
$object->setAttribute('height', '100%');
$embed = $object->getElementsByTagName('embed')->item(0);
$embed->setAttribute('width', '100%');
$embed->setAttribute('height', '100%');
echo $dom->saveXML($dom->getElementsByTagName('body')->item(0));
You probably don't want the element wrapped in body
, so do this...
$objectHtml = '';
foreach($chunk->childNodes as $node) {
$objectHtml .= $dom->saveXML($node, LIBXML_NOEMPTYTAG);
}
Where $chunk
is $dom->getElementsByTagName('body')->item(0)
.
I added the option LIBXML_NOEMPTYTAG
, otherwise if you have <span></span>
it will be turned into <span />
.
If using >= PHP 5.3, use saveHTML()
instead.
Upvotes: 5
Reputation:
Is that all that your string will contain? If so, you can use this:
$replace = preg_replace(
"/(width|height)=\".*?\"/",
"\${1}=\"100%\"",
$html
);
You can tweak the pattern to allow for whitespace between the attribute, =
sign, and value as necessary.
If your string will contain more HTML, with their own width/height attributes you don't want replaced, then you'll need a more robust solution.
Upvotes: 0
Reputation: 2052
$html = '<object width="562" height="200"><param name="movie" value="http://www.youtube.com/v/rBa5qp9sUOY?version=3"><param name="allowFullScreen" value="true"><param name="allowscriptaccess" value="always"><embed src="http://www.youtube.com/v/rBa5qp9sUOY?version=3" type="application/x-shockwave-flash" width="562" height="200" allowscriptaccess="always" allowfullscreen="true"></object>';
$pattern = '/(<object[^>]*width=)\".*?(\"[^>]*height=)\".*?(\"[^>]*>.*?<embed[^>]*width=)\".*?(\"[^>]*height=)\".*?(\"[^>]*>.*?<\/object>)/si';
$replace = '\1"100\2"200\3"300\4"400\5';
echo preg_replace($pattern, $replace, $html);
This should work. Set, 100, 200, 300, 400 to your values.
Upvotes: -1