user7133318 user7133318 - 8 months ago 49
PHP Question

PHP preg_match exact match and get whats inside brackets

Lets say I have the following string:


And I want to use preg_match to find the EXACT text
but the text inside the brackets changes all the time. I used to use:

preg_match('#\((.*?)\)#', $text3, $match2);

Which would get what is inside the brackets which is good but if I had:
it would get whats inside of that. So how can i get whats inside the brackets if, in front of the brackets has

Answer Source

~(?:\*\*link\(([^\)]+)\))~ will match contents in the brackets for all inputs that look like **link(URL) but do not contain extra ) inside URLs. See the example on Regexr: . The whole example:

$text = '"**link(*{Google}**"
**link(*{official Arduino site}';
$regex = '~(?:\*\*link\((?<url>[^)]+))~';
preg_match_all($regex, $text, $matches);

var_dump($regex, $matches['url']);