tags:

views:

61

answers:

3

I have the following line:

<?php echo $this->__("mytext");?>somesometext")moretext

and I need a regular expression to grab 'mytext'. The best I could come up with is:

/\$this->__\([\'"](.*)[\'"]\)/

but in this case it returns:

mytext");?>somesometext

Can anyone get this to work?

+2  A: 
/\$this->__\([\'"](.*?)[\'"]\)/

should work. The ? switches the match-mode to ungreedy.

Stefan Gehrig
+2  A: 
/\$this->__\([\'"](.*?)[\'"]\)/

The ? makes the * quantifier ungreedy.

Richard Nguyen
Both this and Stefan's answers are correct. Picked the one that needed the rep the most.
Manos Dilaverakis
+3  A: 

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.

Gumbo
Even though impractical for my application, +1 for leveraging PHP tokenizer's power.
Manos Dilaverakis