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

Question:
Using PHP, how can I search a string of characters for a URL, save that URL as a variable, and then delete the URL from the string?

Here is my problem:
I am designing a WordPress site and have a box on the homepage with the latest podcast of my client. When reading the Podcast post, a plugin changes the URL of the podcast into an HTML5 audio player. But, on the homepage this plugin doesn’t work and only shows the URL unless I use their PHP short code.

Read More

What I need to do: I need to take the content of the post and save it as a variable. I do this like so:

$content = get_the_content();

Then, I need to parse the variable $content to find the URL for the podcast (should be the first thing in the string), save that URL as a variable and then delete the URL from $content. (I don’t know how to do this.)

Then, I need to replace that URL with the shortcode and print out the rest of the podcast text. Like this:

<?php 
$podcastURL = //Code I do not know how to write// ;

echo do_shortcode(''); 

echo $content;

?>

Things I know
– URL should be the first thing in the string. I can tell my client that this is needed.
– URL will always start with: http://mcmaz.co/podcasts/.
– URL will always end with: .mp3.
– URL should be formatted like this: http://mcmaz.co/podcasts/2014/08/19/Getting_Your_Home_in_Order.mp3

Things I do not know
– The part of the URL after /podcasts/ and before .mp3.
– User error in putting www. before mcmaz.co.

Resources
– Site homepage with broken player: http://www.staticchurch.com
– Sample Podcast Page with working plugin: http://staticchurch.com/podcast/getting-your-home-in-order/

Related posts

Leave a Reply

2 comments

  1. $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);
    
  2. using preg_match_all('/http://mcmaz.co/podcasts/.+.mp3/', $content, $matches); will return the matches.

    Meanwhile preg_replace('/http://mcmaz.co/podcasts/.+.mp3/', '', $content); will replace all the urls with nothing.

    Then it’s a matter of doing a foreach on the podcast code with the matches + with some extra work it’ll be done.