开发者

php regular expressions catch between quotes [closed]

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜