preg_replace 属性值
因此,我有一个有效的 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() 函数中使用相同的模式,它将删除整个
非常感谢任何帮助。
编辑:蒂姆的解决方案如下,
我对蒂姆的模式进行了一些小修改:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要捕获要替换的字符串周围的匹配:
由于您想用不同的值替换
height
和width
,因此您需要两个正则表达式,依次应用:You need to capture the match around the string to be replaced:
Since you want to replace
height
andwidth
with different values, you need two regexes, applied sequentially: