i want to use preg_match to find text between bracket for example: $varx = "(xx)";
final output will be $match = 'xx';
another example $varx = "bla bla (yy) bla bla";
final output will be something like this $match = 'yy';
in other words it strips out bracket. i'm still confusing with regular expression but found that sometimes preg match is more simple solution. searching other example but not meet my need.
Try something like this:
preg_match('/(?<=\()(.+)(?=\))/is', $subject, $match);
Now it will capture newlines.
Bear in mind that brackets are special characters in RegExps so you'll need to escape them with a backslash - you've also not made it clear as to what possible range of characters can appear between ( ... )
or whether there can be multiple instances of ( ... )
.
So, your best bet might be a RegExp like: /\(([^\)]*)\)/
which will match many occurrences of ( ... )
containing any (or no) characters between the parentheses.
Try preg_match('/\(([^\)]*)\)/', $sString, $aMatches)
EDIT: (example)
<?php
$sString = "This (and) that";
preg_match_all('/\(([^\)]*)\)/', $sString, $aMatches);
echo $aMatches[1][0];
echo "\n\n\n";
print_r($aMatches);
?>
Which results in:
and
Array
(
[0] => Array
(
[0] => (and)
)
[1] => Array
(
[0] => and
)
)
So the string "and" is stored in $aMatches[1][0] :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With