php - Wordpress get the parameter of the first shortcode in the content -


i writing script find first occurrence of following shortcode in content , url parameter of shortcode.

the shortcode looks

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

and have done is

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

and 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 string need url of parameter of shortcode

$html = 'our 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 colossal 2-track ep , we\'re getting exclusive sneak-premiere of ep\'s diabolical techno b-side called "hush hush" before 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 colossal 2-track ep , we\'re getting exclusive sneak-premiere of ep\'s diabolical techno b-side called "hush hush" before released tomorrow on <a href="https://www.facebook.com/dimmakrecs">dim mak records</a>! '; 

i guess not best way it. if can guide me how can great. want extract first occurrence of soundcloud url content.

so here's came with:

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

let's explain regex:

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

online php demo                 online regex demo


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -