php preg_replace 对象内部的宽度和高度?

发布于 2024-10-31 12:31:22 字数 2050 浏览 3 评论 0原文

嘿伙计们, 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 $htmlholds 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

错々过的事 2024-11-07 12:31:23

修改 HTML 时,通常最好使用 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));

您可能不希望元素包裹在 body 中,所以这样做...

$objectHtml = '';
foreach($chunk->childNodes as $node) {
    $objectHtml .= $dom->saveXML($node, LIBXML_NOEMPTYTAG);
}

其中 $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.

$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.

东京女 2024-11-07 12:31:23

这就是你的字符串将包含的全部内容吗?如果是这样,您可以使用以下方法:

$replace = preg_replace(
            "/(width|height)=\".*?\"/",
            "\${1}=\"100%\"",
            $html
          );

您可以根据需要调整模式以允许属性、= 符号和值之间存在空格。

如果您的字符串将包含更多 HTML,并且您不想替换它们自己的宽度/高度属性,那么您将需要更强大的解决方案。

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.

耀眼的星火 2024-11-07 12:31:23
preg_replace('/(width|height)(=)"([\d]+)"/', '${1}${2}"100%"', $html);

这应该可以解决问题:)

preg_replace('/(width|height)(=)"([\d]+)"/', '${1}${2}"100%"', $html);

That should do the trick :)

℡Ms空城旧梦 2024-11-07 12:31:23
$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);

这应该有效。
将 100、200、300、400 设置为您的值。

$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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文