使用短代码搜索iframe替换

时间:2013-11-19 14:33:38

标签: php mysql wordpress preg-replace soundcloud

我正在尝试将所有iframe从数据库转换为wordpress短代码。这是一个示例字符串

<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>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&amp;auto_play=false&amp;show_artwork=true&amp;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个小时,但找不到解决方案。因此,如果任何专家可以帮助我完成这项工作,那么我将非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

要说明soundcloud iframe和其他iframe之间的区别,您可以将其设置为“搜索关键字”“搜索和替换”以查找所有实例并将其替换为您想要的任何内容。

同样,请检查this thread。它不是专门针对wordpress的,但你可以看到它如何处理mysql数据库中的查找和替换。