php regular expressions catch between quotes [closed]
I have created this
echo "<p>".preg_replace("/\'[^\)]+\'/","",$line)."</p>";
to get the words between the single quotes "Privacy settings" from this line
$_lang['privacy.settings'] = 'Privacy settings';
but I get 开发者_JS百科this output
$_lang[
I can't figure it out. Regex is so complicated.
The + is greedy in your regex, so it will match the string as long as it can. You can fix this with:
preg_replace("/\'[^\)\']+\'/","",$line)
or
preg_replace("/\'[^\)]+?\'/","",$line)
The ? in the second example tells the regex that + should not be greedy.
If you are trying to get the value out of that line, then try:
echo "<p>" . preg_replace("/^.*=.*\'(.+)\'.*$/", "$1", $line) . "</p>";
精彩评论