【发布时间】:2013-11-19 14:33:37
【问题描述】:
我正在尝试将所有 iframe 从数据库转换为 wordpress 简码。这是一个示例字符串
<iframe src="http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F59701079&auto_play=false&show_artwork=true&color=00adee" frameborder="no" scrolling="no" width="100%" height="166"></iframe>came over from Berlin for a couple of days. Mad fun that guy is!! We actually made 2 tracks during his stay here. The other one will be released soon.<iframe src="http://www.youtube.com/embed/z3fIsvQDrqI" frameborder="0" width="460" height="259"></iframe>\r\n<strong><span style="color: #3366ff;">GDD:</span></strong> So wild! Well we''re very happy you''re getting the deserved attention! Tell us what you have coming up next in the Kill Frenzy world!\r\n<span style="color: #ff0000;"><strong>KF:</strong></span><iframe src="http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F53146049&auto_play=false&show_artwork=true&color=00adee" frameborder="no" scrolling="no" width="100%" height="166"></iframe>
基本上我要做的是转换
<iframe src="http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F59701079&amp;auto_play=false&amp;show_artwork=true&amp;color=00adee" frameborder="no" scrolling="no" width="100%" height="166"></iframe>
到
[soundcloud url="http://api.soundcloud.com/tracks/59701079"]
所以最终字符串会被转换成这样的东西
[soundcloud url="http://api.soundcloud.com/tracks/59701079"] came over from Berlin for a couple of days. Mad fun that guy is!! We actually made 2 tracks during his stay here. The other one will be released soon.<iframe src="http://www.youtube.com/embed/z3fIsvQDrqI" frameborder="0" width="460" height="259"></iframe>\r\n<strong><span style="color: #3366ff;">GDD:</span></strong> So wild! Well we''re very happy you''re getting the deserved attention! Tell us what you have coming up next in the Kill Frenzy world!\r\n<span style="color: #ff0000;"><strong>KF:</strong></span>[soundcloud url="http://api.soundcloud.com/tracks/5314604"]
其实我已经问过类似的问题here
当字符串中只有一个 iframe 时有效。当有多个 iframe 时,它会显示一些奇怪的结果。当有其他 iframe 像 youtube、vimeo 时,它也显示出问题。我浪费了大约 10 个小时,但找不到解决方案。因此,如果任何专家可以帮助我完成这项工作,我将非常感谢您的帮助。
【问题讨论】:
-
不要在 html 上使用正则表达式。使用 dom 查找 iframe,并将它们替换为包含您的简码的文本节点。
-
是的,但我无法弄清楚我究竟是如何做到这一点的。这只是一次代码。
标签: php mysql wordpress preg-replace soundcloud