昨天,我发布了一个关于“Hom to change a DIV id with a php function”的问题,有人用正则表达式找到了正确的答案。
问题是,我想用这个答案把这个方法应用到其他同样的问题上。
但是,因为我不擅长正则表达式,所以我不能。
所以问题是我用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。
如您所见,内容具有以下ID:
div id="player959093-parent"
div id="player959093"
div id="player959093-config
s1.write("player959093");

所以我的问题是:是否有一个函数可以替换字符串“player959093”或将其与其他字符串连接
字符串来解决显示问题?
非常感谢你,
当做。

最佳答案

尝试使用preg_replace_回调函数:

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

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

07-24 15:21
查看更多