开发者

regular expression to grab quoted text in PHP code

开发者 https://www.devze.com 2022-12-11 01:13 出处:网络
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

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.

0

精彩评论

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