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
[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> & <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.
So here’s what I came up with:
Let’s explain the regex:
Online PHP demo
Online regex demo