开发者

PHP Regular Expression to match define() function

I'm doing a translation of php script and want to gain some time by doing some automatization The file contains very large number of lines like these

define('LNG_NewsletterNameIsNotValid', 'Email Campaign name is not Valid');
define('LNG_UnableToCreateNewsletter', 'Unable to create an Campaña de Email');
define('LNG_HLP_NewsletterName', 'You must insert \\\'Custom Fields\\\' in the field.');
define('LNG_UnableToUpdateNewsletter', 'Unable to update '. LNG_xxx . ' from Email Campaign');

and I would like to:

  • open each php file
  • go through each line
  • get the string corresponding to each define() 开发者_开发问答
  • put a field where I should write the translation
  • save the new string to the file

I have the script almost done, except for the part for getting the string to be translated from each line. I point several examples so you can see that translation strings can contain references to other variables, quotes, etc.

Do I need a regular expression, right?

Can anyone help me with one?


You'd probably be better of using token_get_all. While regex could probably get most of them, It doesn't have the necessary complexity to parse PHP correctly (just like regexes can't parse html). token_get_all would only miss edge cases, such as using a variable-function or eval'ing text.

If your doing language translations it might be worth looking into gettext instead of using define.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜