Preg Replace 视频对象代码 - 仅适用于第一个实例

发布于 2024-10-19 20:42:38 字数 894 浏览 8 评论 0原文

任何人都可以帮助我解释为什么以下代码仅适用于目标代码中宽度/高度的第一个实例。我需要替换使用“height: xxx ; width : xxx”的样式,然后替换实际嵌入的高度=“xxx”和宽度=“xxx”

这是预替换:

$vid_object = preg_replace('~((width)\s?[=:]\s?[\'"]?)[0-9]+~i','${1}600',$vid_object);

$vid_object = preg_replace('~((height)\s?[=:]\s?[\'"]?)[0-9]+~i','${1}364',$vid_object); 

这是视频输出:

<object style="height: 364px; width: 600px"><param name="movie" value="http://www.youtube.com/v/UF06-2Q0-G0?version=3"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/UF06-2Q0-G0?version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="390"></object>

我认为它有与代码“${1}600”的替换部分有关,或者结果仅限于第一个实例。

非常感谢任何帮助。当正则表达式不是我的专长时,浪费了太多时间。 谢谢 尼古拉斯

Can anyone help me decipher why the following code only works on the first instance of width / height in an object code. I need to replace both the style which use "height: xxx ; width : xxx" and then also replace the actual embed's height = "xxx" and width = "xxx"

here's the preg replace:

$vid_object = preg_replace('~((width)\s?[=:]\s?[\'"]?)[0-9]+~i','${1}600',$vid_object);

$vid_object = preg_replace('~((height)\s?[=:]\s?[\'"]?)[0-9]+~i','${1}364',$vid_object); 

here's the video output:

<object style="height: 364px; width: 600px"><param name="movie" value="http://www.youtube.com/v/UF06-2Q0-G0?version=3"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/UF06-2Q0-G0?version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="390"></object>

I think it has something to do with the replacement part of the code '${1}600' or that the results are limited to first instance.

Any help is greatly appreciated. Wasted too much time hunting when regex isn't my specialty.
Thanks
nicholas

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

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

发布评论

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

评论(2

爱,才寂寞 2024-10-26 20:42:38

尝试一下

$vid_object=preg_replace('/(width[\s:=\'"]+)\d+/i','${1}600',$vid_object);
$vid_object=preg_replace('/(height[\s:=\'"]+)\d+/i','${1}364',$vid_object);

范围更广一些。

当我在你提供的代码上运行它时,我得到了

<object style="height: 364px; width: 600px"><param name="movie" value="http://www.youtube.com/v/UF06-2Q0-G0?version=3"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/UF06-2Q0-G0?version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="600" height="364"></object>

try

$vid_object=preg_replace('/(width[\s:=\'"]+)\d+/i','${1}600',$vid_object);
$vid_object=preg_replace('/(height[\s:=\'"]+)\d+/i','${1}364',$vid_object);

It's a little broader.

When I run that on the code you provided I get

<object style="height: 364px; width: 600px"><param name="movie" value="http://www.youtube.com/v/UF06-2Q0-G0?version=3"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/UF06-2Q0-G0?version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="600" height="364"></object>
窗影残 2024-10-26 20:42:38

正如沙德所说,这确实是魔法引号的问题。我刚刚在 $_POST 数据上使用了 stripslahes。

Just as Shad suggested this was indeed a problem with the magic quotes being on. I just used the stripslahes on the $_POST data.

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