WordPress get the parameter of the first shortcode in the content

I am writing a script to find the first occurrence of the following shortcode in content and then get the url parameter of the shortcode.

the shortcode looks like this

Read More
[soundcloud url="http://api.soundcloud.com/tracks/106046968"]

and what i have currently done is

$pattern = get_shortcode_regex();
  $matches = array();
  preg_match("/$pattern/s", get_the_content(), $matches);
  print_r($matches);

and the result looks like

Array (
    [0] => [soundcloud url="http://api.soundcloud.com/tracks/106046968"]
    [1] =>
    [2] => soundcloud
    [3] => url="http://api.soundcloud.com/tracks/106046968"
    [4] =>
    [5] =>
    [6] =>
)

Here is the string from which i need the url of the parameter of the shortcode

$html = 'Our good homies <a href="https://www.facebook.com/yungskeeter">DJ Skeet Skeet aka Yung Skeeter</a> &amp; <a href="https://www.facebook.com/WaxMotif">Wax Motif</a> have teamed up to do a colossal 2-track EP and we're getting the exclusive sneak-premiere of the EP's diabolical techno b-side called "Hush Hush" before its released tomorrow on <a href="https://www.facebook.com/dimmakrecs">Dim Mak Records</a>!

[soundcloud url="http://api.soundcloud.com/tracks/104477594"]
<a href="https://www.facebook.com/WaxMotif">Wax Motif</a> have teamed up to do a colossal 2-track EP and we're getting the exclusive sneak-premiere of the EP's diabolical techno b-side called "Hush Hush" before its released tomorrow on <a href="https://www.facebook.com/dimmakrecs">Dim Mak Records</a>!
';

I guess this is not the best way to do it. If can guide me how we can do this then it would be great. Basically i want to extract the first occurrence of soundcloud url from the content.

Related posts

Leave a Reply

1 comment

  1. So here’s what I came up with:

    preg_match('~[soundclouds+urls*=s*("|')(?<url>.*?)1s*]~i', $input, $m); // match
    
    print_r($m); // print matches (groups) ...
    $url = isset($m['url']) ? $m['url']:''; // if the url doesn't exist then return empty string
    echo 'The url is : ' . $url; // Just some output
    

    Let’s explain the regex:

    ~                   # set ~ as delimiter
    [soundcloud        # match [soundcloud
    s+                 # match a whitespace 1 or more times
    url                 # match url
    s*                 # match a whitespace 0 or more times
    =                   # match =
    s*                 # match a whitespace 0 or more times
    ("|')              # match either a double quote or a single quote and put it in group 1
    (?<url>.*?)         # match everything ungreedy until group 1 is found and put it in a named group "url"
    1                  # match what was matched in group 1
    s*                 # match a whitespace 0 or more times
    ]                  # match ]
    ~                   # delimiter (end expression)
    i                   # set the i modifier, which means match case-insensitive
    

    Online PHP demo
                   
    Online regex demo