preg_replace 属性值

发布于 2024-10-22 02:06:45 字数 1525 浏览 2 评论 0原文

因此,我有一个有效的 preg_match 模式,用于查找字符串中 HTML 属性的值,并且我需要使用 preg_replace 执行相同的操作 - 不是查找并返回该值,而是将其替换为新值。

但如果我只使用 preg_match 模式,它就不起作用......我被困住了。

代码如下:

    $string = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
width="437" height="288" id="viddler">
<param name="movie" value="http://www.viddler.com/player/1234567890/" />
<param name="allowScriptAccess" value="always" />
<param name="allowFullScreen" value="true" />
<param name="flashvars" value="fake=1"/>
<embed src="http://www.viddler.com/player/1234567890/" 
width="437" height="288" type="application/x-shockwave-flash" 
allowScriptAccess="always" allowFullScreen="true" flashvars="fake=1" 
name="viddler" ></embed></object>';

preg_match("|<*[^>]*width=\"([^>]+)\"[^>]*>|Ui", $string, $results);

echo 'Width: '.$results[1];

这有效并将返回 437。如果我在 preg_replace() 函数中使用相同的模式,它将删除整个 标记。我需要用其他值替换 437,比如 200。当我们这样做时,我还需要替换高度值。最后,这应该适用于任何嵌入代码或 iframe,因此它应该分别查找 width="height="...就像上面的 preg_match 示例一样。

非常感谢任何帮助。

编辑:蒂姆的解决方案如下,

我对蒂姆的模式进行了一些小修改:

$interm = preg_replace('|(<*[^>]*width=)"[^>]+"([^>]*>)|Ui', '\1"200"\2', $string);
$result = preg_replace('|(<*[^>]*height=)"[^>]+"([^>]*>)|Ui', '\1"300"\2', $interm);

如果没有此更改,第一个函数将替换宽度,但它会取出标签中此后的每个属性。

感谢蒂姆提供的解决方案!

So I have a working preg_match pattern for finding the value of an HTML attribute in a string, and I need to do the same thing with preg_replace - not find and return the value, but replace it with a new one.

But it doesn't work if I just use the preg_match pattern... I'm stuck.

Here's the code:

    $string = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
width="437" height="288" id="viddler">
<param name="movie" value="http://www.viddler.com/player/1234567890/" />
<param name="allowScriptAccess" value="always" />
<param name="allowFullScreen" value="true" />
<param name="flashvars" value="fake=1"/>
<embed src="http://www.viddler.com/player/1234567890/" 
width="437" height="288" type="application/x-shockwave-flash" 
allowScriptAccess="always" allowFullScreen="true" flashvars="fake=1" 
name="viddler" ></embed></object>';

preg_match("|<*[^>]*width=\"([^>]+)\"[^>]*>|Ui", $string, $results);

echo 'Width: '.$results[1];

This works and will return 437. If I use the same pattern in a preg_replace() function though, it will strip out the entire <object> tag. I need to replace that 437 with anything else, say 200. And while we're at it, I need to replace the height value as well. Finally, this should work for any embed code or iframe, so it should just look for width=" or height=", respectively... just like the preg_match example above.

Any help is much appreciated.

EDIT: Tim's solution below

I got Tim's pattern to work with a small modification:

$interm = preg_replace('|(<*[^>]*width=)"[^>]+"([^>]*>)|Ui', '\1"200"\2', $string);
$result = preg_replace('|(<*[^>]*height=)"[^>]+"([^>]*>)|Ui', '\1"300"\2', $interm);

Without this change, the first function replaces the width, but it takes out every attribute after that in the tag.

Thanks to Tim for the solution!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

長街聽風 2024-10-29 02:06:45

您需要捕获要替换的字符串周围的匹配:

由于您想用不同的值替换heightwidth,因此您需要两个正则表达式,依次应用:

$interm = preg_replace('/(<*[^>]*width=)"[^>]+"([^>]*>)/', '\1"200"\2', $subject);
$result = preg_replace('/(<*[^>]*height=)"[^>]+"([^>]*>)/', '\1"300"\2', $interm);

You need to capture the match around the string to be replaced:

Since you want to replace height and width with different values, you need two regexes, applied sequentially:

$interm = preg_replace('/(<*[^>]*width=)"[^>]+"([^>]*>)/', '\1"200"\2', $subject);
$result = preg_replace('/(<*[^>]*height=)"[^>]+"([^>]*>)/', '\1"300"\2', $interm);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文