Replace a div content with PHP
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.
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>
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");
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.
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.
Why don't you use the concanation operator dot '.'
"a" . "b" = "ab"
精彩评论