我正在构建一个任务(在 PHP 中),它读取我项目的所有文件以搜索 i18n 消息。我想检测这样的消息:
// Basic example
__('Show in English') => Show in English
// Get the message and the name of the i18n file
__("Show in English", array(), 'page') => Show in English, page
// Be careful of quotes
__("View Mary's Car", array()) => View Mary's Car
// Be careful of strings after the __() expression
__('at').' '.function($param) => at
适用于这些情况的正则表达式(考虑到其他一些情况)是:
__\(.*?['|\"](.*?)(?:['|\"][\.|,|\)])(?: *?array\(.*?\),.*?['|\"](.*?)['|\"]\)[^\)])?
但是,如果表达式在多行中,则它不起作用。我必须包括 dotail /s
,但它破坏了以前的正则表达式,因为它不能很好地控制何时停止向前看:
// Detect with multiple lines
echo __('title_in_place', array(
'%title%' => $place['title']
), 'welcome-user'); ?>
有一件事可以解决问题并简化匹配开闭括号的正则表达式。因此,无论里面有什么__()
或有多少括号,它都会“计算”打开的数量并期望关闭的数量。
可能吗?如何?非常感谢!