views:

27

answers:

1

Can't seem to figure this one out. Just trying to preg match for a specific variable name in a link URL:

<a href="http://something.com?variable=name"&gt;GenericLink&lt;/a&gt;

How do I get the variable=name out of this?

Thanks!

+3  A: 

Extract the whole url and then use parse_url();

$str = '<a href="http://something.com?variable=name"&gt;GenericLink&lt;/a&gt;';

preg_match('/href="([^"]*)/i',$str,$matches);
var_dump(parse_url($matches[1]));
Mark Baker
ahh, didn't think of that. good tip.
One more step: `$url = parse_url($matches[1]); parse_str($url['query'], $vars); var_dump($vars['variable']);`
Frank Farmer
$url = parse_url($matches[1], PHP_URL_QUERY); parse_str($url, $vars);
Mark Baker