用PHP替换div内容

时间:2010-03-19 09:07:35

标签: php regex html

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

问题在于我想使用这个答案来解决其他相同问题的方法。

但是,由于我在正则表达方面非常糟糕,所以我不能。

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

<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。 如您所见,内容包含以下内容:

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

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

非常感谢, 问候。

2 个答案:

答案 0 :(得分:2)

尝试使用preg_replace_callback函数:

$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);

我没有测试过代码,但它应该可以运行。

答案 1 :(得分:0)

为什么不使用concanation operator dot'。'

"a" . "b" = "ab"
相关问题