Q:

How to get attribute from bbcode [URL] tag using regex

I have a string 
$string = '[URL="http://www.google.com"]Google[/URL]

I am using the following code to convert it to html link
$link = preg_replace('/\[url=(.+?)\](.+?)\[\/url\]/i', '<a href="\1">\2</a>', $string);

it converts it to the following format
<a href="http://www.google.com">Google</a>

That is OK. But I want to validate the both values http://www.google.com and Google, How can I get both the values separately with the Help of Regex.

A:

I think what you want is preg_match?
Actually, you can just use the pattern you already have. It will give you the whole match and each match within round parantheses through a parameter.
preg_match('/\[url=(.+?)\](.+?)\[\/url\]/i', $string, $matches);
echo $matches[1]; // http://www.google.com
echo $matches[2]; // Google
echo $matches[0]; // [URL="http://www.google.com"]Google[/URL]

The return value of preg_match is 1 (match found) or 0/false (no match/error).

