regular expression to grab quoted text in PHP code
I have the following line:
<?php echo $this->__("mytext");?>somesometext")moretext
and I need a regular expression to gra开发者_StackOverflowb 'mytext'. The best I could come up with is:
/\$this->__\([\'"](.*)[\'"]\)/
but in this case it returns:
mytext");?>somesometext
Can anyone get this to work?
Better use PHP’s ability to parse its own code with token_get_all
, step through the tokens and stop at the first T_CONSTANT_ENCAPSED_STRING
token.
/\$this->__\([\'"](.*?)[\'"]\)/
The ?
makes the *
quantifier ungreedy.
/\$this->__\([\'"](.*?)[\'"]\)/
should work. The ?
switches the match-mode to ungreedy.
精彩评论