0

I have to create translations for the project I work on. The simplest solution was to change all stings to a functioncall(string) so that I could get unique string hashes everytime.

My code has the following different t() function uses:

<label for="anon"><?php echo t('anonymously? (20% extra)')?></label>

exit(t("Success! You made {amount} oranges out of it!", array('amount' => $oranges)));

echo t('You failed.');

My current regexp is:

$transMatches = preg_match_all('/[^a-z]t\([^)(]+/m', $contents, $matches);

The problem is that it fails on #1 example, matchin "anonymously?". What I really want to achieve is: "match t( then match either ' or " then match anything except what you matched for ' or " and )"

Idea: t\(['|"](.*?)[^'|"]\)?

I cannot make above regexp to work.

How could I do AND in regexp so that it matches "['|"] AND )" OR "['|"] AND, array"

Please help me on regexp and explain why it works.

Thank you!

Vyktor
  • 20,559
  • 6
  • 64
  • 96
Aivaras
  • 429
  • 4
  • 16

2 Answers2

1

Parsing function arguments may be quite complex, but you need to parse only the first argument which (for simplicity) can assume always to be string escaped either with ' or with ", thus those regexps may match"

"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"
\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*'

Therefore you just need to match:

'~[^\w\d]t\(\s*("[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\')~i'

[^\w\d] assumes that no test1t will match, \s* makes you space tolerant...

With this regexp you'll get results like:

'anonymously? (20% extra)'
"Success! You made {amount} oranges out of it!"

And I can't imagine situation where you would need to parse out array too, can you describe it in comment/question?

Community
  • 1
  • 1
Vyktor
  • 20,559
  • 6
  • 64
  • 96
  • Thanks for answer, will test your regexp, it look tough though. I do not want to match array, it was just a case when my regexp match should stop, sorry if I didn't point it out well. – Aivaras Dec 08 '12 at 10:27
1

Is this what you need?

Using Backreferences in The Regular Expression - http://www.regular-expressions.info/brackets.html

But it looks strange what are you doing, and why? Are you replacing the function call with some result? why dont you just let it call the function and return translation from it?

premek.v
  • 231
  • 4
  • 14
  • Thanks for link! Great to remember this reference :) Will surely test it out. What I'm doing is collecting strings that are in code so that I could populate db with current strings and then prepare translations before my users see output. – Aivaras Dec 08 '12 at 10:25