php preg_replace 对象内部的宽度和高度?
嘿伙计们, php 变量 $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>
知道如何过滤对象标签和嵌入标签的宽度和高度以获取 100% 值吗?
<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>`
感谢您的帮助!
更新:
$chunk = $dom->getElementsByTagName('body')->item(0);
$objectHtml = '';
foreach($chunk->childNodes as $node) {
$objectHtml .= $node->saveXML();
}
return $objectHtml;
说:调用未定义的方法 DOMElement::saveXML()
而...
$dom = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveXML()));
工作正常!然而,当使用这个时,我在源代码中插入了奇怪的
。
像这样:
<!--?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>
知道我在这里做错了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
修改 HTML 时,通常最好使用 HTML 解析器而不是正则表达式。
您可能不希望元素包裹在
body
中,所以这样做...其中
$chunk
是$dom->getElementsByTagName('body') -> 项目(0)
。我添加了选项
LIBXML_NOEMPTYTAG
,否则如果您有,它将变成
>。
如果使用 >= PHP 5.3,请使用
saveHTML()
相反。It is often better to use a HTML parser instead of a regex when modifying HTML.
You probably don't want the element wrapped in
body
, so do this...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.这就是你的字符串将包含的全部内容吗?如果是这样,您可以使用以下方法:
您可以根据需要调整模式以允许属性、
=
符号和值之间存在空格。如果您的字符串将包含更多 HTML,并且您不想替换它们自己的宽度/高度属性,那么您将需要更强大的解决方案。
Is that all that your string will contain? If so, you can use this:
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.
这应该可以解决问题:)
That should do the trick :)
这应该有效。
将 100、200、300、400 设置为您的值。
This should work.
Set, 100, 200, 300, 400 to your values.