使用PHP,在字符串中搜索URL并将该URL保存为值


Using PHP, search a string of characters for a URL and save that URL as a value

问题:
使用PHP,如何在字符串中搜索URL,将该URL保存为变量,然后从字符串中删除该URL?

这是我的问题:
我正在设计一个WordPress网站,主页上有一个盒子,里面有我客户的最新播客。当阅读播客帖子时,一个插件会将播客的URL更改为HTML5音频播放器。但是,在主页上,这个插件不起作用,只显示URL,除非我使用他们的PHP短代码。

我需要做的:我要获取帖子的内容并将其保存为变量。我是这样做的:

$content = get_the_content();


然后,我需要解析变量$content以找到播客的URL(应该是字符串中的第一个),将该URL保存为变量,然后从$content中删除该URL(我不知道怎么做。)

然后,我需要用短代码替换该URL,并打印出播客文本的其余部分。像这样:

<?php 
$podcastURL = //Code I do not know how to write// ;
echo do_shortcode('[audio mp3="'.$podcastURL.'" autoplay="off" loop="off" preload="on" controls="on" flash_fallback="on"]'); 
echo $content;
?>


我知道的事情
-URL应该是字符串中的第一个内容。我可以告诉我的客户这是必要的
-URL将始终以:http://mcmaz.co/podcasts/开头
-URL将始终以:.mp3结尾
-URL的格式应该如下:http://mcmaz.co/podcasts/2014/08/19/Getting_Your_Home_in_Order.mp3


我不知道的事情
-/podcasts/之后和.mp3之前的URL部分
-用户将www.置于mcmaz.co之前时出错。

资源
-网站主页与破碎的播放器:http://www.staticchurch.com
-带有工作插件的示例播客页面:http://staticchurch.com/podcast/getting-your-home-in-order/

$text = 'http://mcmaz.co/podcasts/2014/08/19/Getting_Your_Home_in_Order.mp3 Guest speaker Pastor Rick Holman over the Cactus campus';
$start = 'http';
$end = 'mp3';
function getMiddle($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}
$middle = getMiddle($text, $start, $end);
//Saved url variable
$url = $start.''.$middle.''.$end;
//Text with removed url
$text = str_replace($url, '', $text);

使用preg_match_all('/http':'/'/mcmaz'.co'/podcasts'/.+'.mp3/', $content, $matches);将返回匹配项。

同时preg_replace('/http':'/'/mcmaz'.co'/podcasts'/.+'.mp3/', '', $content);将用nothing替换所有url。

然后是在播客代码上用火柴做一个foreach的问题,再加上一些额外的工作。