开发者

php regular expressions catch between quotes [closed]

开发者 https://www.devze.com 2023-01-27 00:38 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

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>";
0

精彩评论

暂无评论...
验证码 换一张
取 消