gpt4 book ai didi

php - preg_match 查找并替换字符串模式

转载 作者:可可西里 更新时间:2023-11-01 13:45:20 27 4
gpt4 key购买 nike

我有一个 wordpress 数据库,其中有一些来自 sound cloud 的嵌入式 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&amp;color=000000&amp;auto_play=false&amp;show_artwork=true" frameborder="no" scrolling="no" width="100%" height="350"></iframe>';

preg_match("/url=(.*?)/", $string, $matches);

print_r($matches);

上面的代码不起作用,我对正则表达式不太熟悉,所以如果有人能弄清楚这里出了什么问题,那就太好了。而且,如果有人可以指导我执行此操作的正确过程,那就太好了。

最佳答案

由于您在这里使用的是 HTML,我建议您使用 DOM 函数:

$doc = new DOMDocument;
$doc->loadHTML($string);

foreach ($doc->getElementsByTagName('iframe') as $iframe) {
$url = $iframe->getAttribute('src');
// parse the query string
parse_str(parse_url($url, PHP_URL_QUERY), $args);
// save the modified attribute
$iframe->setAttribute('src', $args['url']);
}

echo $doc->saveHTML();

这会输出完整的文档,因此您需要对其进行缩减:

$body = $doc->getElementsByTagName('body')->item(0);
foreach ($body->childNodes as $node) {
echo $doc->saveHTML($node);
}

输出:

<p>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="http://api.soundcloud.com/playlists/8087281" frameborder="no" scrolling="no" width="100%" height="350"></iframe></p>

关于php - preg_match 查找并替换字符串模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18209205/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com