-1

I have PHP scripts that have a function, in the following format i8n('Content'), but I need to find all the strings that are inside i8n.

Using some regular expressions I managed to get results close to what I want.

RegEx: i8n\('(.*)'\)
Content: i8n('First Message.');
Result: First Message.

For this case, RegEx was perfect. The problem occurs when there is other content on the same line and ends up merging the results.

RegEx: i8n\('(.*)'\)
Content: logErrorUser(i8n('First Message.'), 500, date(), i8n('Second Message, same line.'));
Result: First Message.'), 500, date(), i8n('Second Message, same line.

How do I get each one individually?

Tom
  • 465
  • 1
  • 6
  • 15

2 Answers2

1

You should use preg_match_all() function and modify your regular expression:

  1. preg_match_all() would get you all found results;
  2. In your i8n\('(.*)'\) regular expression you use capturing group with .* tokens, which means literally anything. So, it return you all symbols after i8n(' substring. To avoid this, you should exclude end of the line you need to capture from capturing group like this: i8n\('([^']*) so it will return everything after i8n(' substring and before nearest ' symbol.

UPD: you can use this site to build your regexp and test it on the fly. Also, pay attention on preg_match_all() doesn't return array with results, it returns just number of full pattern matches (which might be zero), or false if an error occurred.

Scilef
  • 100
  • 9
1

Just use ungreedy switch (U at the end of regex):

preg_match_all("~i8n\('(.*)'\)~U",$mystr,$matches);
Flash Thunder
  • 10,029
  • 6
  • 39
  • 82