用PHP替换div内容 [英] Replace a div content with PHP

查看:89
本文介绍了用PHP替换div内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天,我发布了一个关于用PHP函数更改DIV ID的问题,并且有人用正则表达式找到了正确的答案。

yesterday, I posted a question about "Hom to change a DIV id with a php function" and someone found the right answer with a regular expression.

问题是我想用这个答案来适用于其他相同问题的方法。

The problem is that I wanted to use this answer to aplly the method on other same problems.

但是,由于我在正则表达式上很糟糕,所以我不能。

But, as I'm very bad at regular expressions, I couldn't.

所以问题是我用FCKEditor上传了一些视频,并将视频脚本放入我的数据库中,结果如下所示:

So the problem is that I upload some videos with FCKEditor and put the video script in my database and the result is like that:

<div id="player959093-parent" style="text-align: center;float: left;">
<div style="border-style: none; height: 160px; width: 270px; overflow: hidden; background-color: rgb(220, 220, 220); background-image: url(http://localhost/fckeditor/editor/plugins/flvPlayer/flvPlayer.gif); background-repeat:no-repeat; background-position:center;"><script src="http://localhost/fckeditor/editor/plugins/flvPlayer/swfobject.js" type="text/javascript"></script>
<div id="player959093"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.
<div id="player959093-config" style="display: none;visibility: hidden;width: 0px;height:0px;overflow: hidden;">url=/editeur/images/flash/FOO.flv width=270 height=160 loop=false play=false downloadable=false fullscreen=true displayNavigation=true displayDigits=true align=left dispPlaylist=none playlistThumbs=false</div>
</div>
<script type="text/javascript">
    var s1 = new SWFObject("http://localhost/editeur/javascript/fckeditor/editor/plugins/flvPlayer/mediaplayer.swf","single","270","160","7");
    s1.addVariable("width","270");
    s1.addVariable("height","160");
    s1.addVariable("autostart","false");
    s1.addVariable("file","/editeur/images/flash/FOO.flv");
    s1.addVariable("repeat","false");
    s1.addVariable("image","");
    s1.addVariable("showdownload","false");
    s1.addVariable("link","/editeur/images/flash/FOO.flv");
    s1.addParam("allowfullscreen","true");
    s1.addVariable("showdigits","true");
    s1.addVariable("shownavigation","true");
    s1.addVariable("logo","");
    s1.write("player959093");
</script></div>
</div>

当我在PHP页面中回复一次这个内容时,它效果很好。不止一次,视频不会出现。由于唯一的ID,这是显而易见的

正如你所看到的,内容有这些ID:

When I echo this content once in my PHP page, It works great. More than once, the videos doesn't appear. Which is obvious because of the unique ID. As you can see, the content has these ids:

div id="player959093-parent"
div id="player959093"
div id="player959093-config
s1.write("player959093");

所以我的问题是:是否有一个函数可以替换字符串player959093或连接它与其他
字符串来解决显示问题?

So my question is: Is there a function that can replace the string "player959093" or concatenate it with some other string to resolve the display problem?

非常感谢您,
问候。

Thank you very much, Regards.

推荐答案

使用preg_replace_callback函数:

Try with the preg_replace_callback function:

$HTMLCODE="Your html code";
$fn=create_function('$matches','$replacement="player".mt_rand();return preg_replace("#player\d+#",$replacement,$matches[0]);');
echo preg_replace_callback("#<div[^>]*id=\"player\d+-parent\".*?</script>\s*</div>\s*</div>#si",$fn,$HTMLCODE);

我没有测试代码,但应该可以工作。

I've not tested the code but it should work.

这篇关于用PHP替换div内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆