【发布时间】:2013-08-13 12:33:05
【问题描述】:
我有一个 wordpress 数据库,其中包含一些来自声音云的嵌入式 iframe。我希望将 iframe 替换为某种短代码。我什至创建了一个简码,效果很好。
问题是我有一个旧数据库,其中包含大约 2000 个已经嵌入代码的帖子。我想要做的是编写一个代码,以便它将 iframe 替换为简码。
这是我用来从内容中查找 url 的代码,但它总是返回空白。
$string = 'Think Kavinsky meets Futurecop! meets your favorite 80s TV show theme song and you might be pretty close to Swedish producer Johan Bengtsson\'s retro project, <a href="https://soundcloud.com/daataa"><strong>Mitch Murder</strong></a>. Title track, "The Touch," is genuinely lighthearted and fun, crossing over from 80s synth work into a bit of French Touch influence; also including a big time guitar solo straight out of your dad\'s record collection. B-side "Race Day" could very easily be the soundtrack to a video montage of all of your favorite beach scenes from every 80s movie you\'ve ever watched, or as the PR put it, "quite possibly a contender to be the title screen music to a Wave Race 64 sequel." Sounds awesome to me. Also included in this package out today on <a href="https://soundcloud.com/maddecent/">Mad Decent</a>\'s Jeffree\'s sub-label are two remixes of the A-side from Lifelike and Nite Sprite. Download below.
<iframe src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F8087281&color=000000&auto_play=false&show_artwork=true" frameborder="no" scrolling="no" width="100%" height="350"></iframe>';
preg_match("/url=(.*?)/", $string, $matches);
print_r($matches);
上面的代码不起作用,我对正则表达式不太熟悉,所以如果有人能弄清楚这里有什么问题,那就太好了。而且,如果有人可以指导我正确的流程来做到这一点,那就太好了。
【问题讨论】:
-
不要使用正则表达式解析 HTML。使用适当的 HTML 解析模块。 您无法使用正则表达式可靠地解析 HTML,并且您将面临悲伤和挫败感。一旦 HTML 与您的期望发生变化,您的代码就会被破坏。有关如何使用已经编写、测试和调试的 PHP 模块正确解析 HTML 的示例,请参阅 htmlparsing.com/php。
-
谢谢安迪,我试试看
标签: php html-parsing preg-replace preg-match