Manos Dilaverakis
Manos Dilaverakis

Reputation: 5879

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 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?

Upvotes: 1

Views: 123

Answers (3)

Gumbo
Gumbo

Reputation: 655815

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.

Upvotes: 3

Richard Nguyen
Richard Nguyen

Reputation: 1331

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

The ? makes the * quantifier ungreedy.

Upvotes: 2

Stefan Gehrig
Stefan Gehrig

Reputation: 83692

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

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

Upvotes: 2

Related Questions