PHP - 正则表达式问题
我有从 PHP 文件中读取的字符串,例如:
<?php
// Dream Portal (c) 2009-2010 Dream Portal Team
// DreamPortal.english.php; @1.1
global $scripturl, $context;
// General Strings
$txt['forum'] = 'Forum';
$txt['dream_portal'] = 'Dream Portal';
$txt['dp_core_modules'] = 'Collapse or Expand this Module';
$txt['dp_who_forum'] = 'Viewing the forum index of <a href="' . $scripturl . '?action=forum">' . $context['forum_name'] . '</a>.';
$txt['dp_who_portal'] = 'Viewing the portal index of <a href="' . $scripturl . '">' . $context['forum_name'] . '</a>.';
$txt['dp_who_page'] = 'Viewing the page "<a href="' . $scripturl . '?page=%1$s">%2$s</a>".';
?>
这是我当前正在使用的 REGEX,它可以满足我的需要:
$data = file_get_contents('./'.$language.'/'.$file.'.'.$language.'.php');
$codes = array (
'/(\' \. \$)(.+?)( \. \')/',
'/(\= \$)(.+?)( \. \')/',
'/(\' \. \$)(.+?)(\;)/',
'/(\[\')(.+?)(\'\])/',
'/<\?php/s', '/\?>/s', '/<\?/s'
);
$html = array (
'{$2}',
'= \'{$2}',
'{$2}\';',
'[$2]',
'',
);
// Since we don't have the values for the vars.
$data = preg_replace($codes, $html, $data);
但是,由于这些字符串不断变化,因此可能需要这样的字符串/插入到任何 $txt 数组变量中:
$txt['dp_who_page'] = 'Viewing the page "<a href="'.$scripturl.'?page=%1$s">%2$s</a>" . ';
变量与 .
聚集在一起,字符串变量的末尾有此 。 ';
这与上面所说的不同,这是这样的:
$txt['dp_who_page'] = 'Viewing the page "<a href="' . $scripturl . '?page=%1$s">%2$s</a>".';
在这两种情况下,我需要它返回上面的 $txt['dp_who_page'] ,如下所示:
查看 {context[forum_name]} 的论坛索引。
目前它不适用于第二种情况。
对于上面列出的两种情况,如何使用上面的以下 preg_replace 代码来执行此操作?需要改变什么?
我正在使用 eval()
php 函数来提取变量。
好的,这是整个功能:
function loadLanguageFile($language, $file) {
$temp = array();
$data = file_get_contents('./'.$language.'/'.$file.'.'.$language.'.php');
$codes = array (
'/(\' \. \$)(.+?)( \. \')/',
'/(\= \$)(.+?)( \. \')/',
'/(\' \. \$)(.+?)(\;)/',
'/(\[\')(.+?)(\'\])/',
'/<\?php/s', '/\?>/s', '/<\?/s'
);
$html = array (
'{$2}',
'= \'{$2}',
'{$2}\';',
'[$2]',
'',
);
// Since we don't have the values for the vars.
$data = preg_replace($codes, $html, $data);
// We must change this because they are global.
$data = str_replace('$txt', '$langEditor_txt', $data);
$data = str_replace('$helptxt', '$langEditor_helptxt', $data);
eval($data);
if (isset($langEditor_txt)) {
$temp['txt'] = $langEditor_txt;
unset($GLOBALS['langEditor_txt']);
}
if (isset($langEditor_helptxt)) {
$temp['helptxt'] = $langEditor_helptxt;
unset($GLOBALS['langEditor_helptxt']);
}
return $temp;
}
您可以到这里看看我想要完成什么,基本上,我想让字符串更容易地翻译成语言,基于这些语言的英文字符串: 使用此函数加载语言的语言测试。点击一种语言(目前只有西班牙语、法语、英语),然后编辑语言,您将看到 preg_replace 生效。另外,如果您编辑 DreamPortal 语言,它就是一个很好的示例,说明了它应该做什么。此外,默认文本文本区域框(在单击语言 View
链接后,然后单击 Create It
链接)正在从实际英语加载英语字符串所有语言的文件。因此 preg_replace 使用它将该特定字符串定义输入到默认文本文本区域中。
I have strings that are being read from a PHP file, for example:
<?php
// Dream Portal (c) 2009-2010 Dream Portal Team
// DreamPortal.english.php; @1.1
global $scripturl, $context;
// General Strings
$txt['forum'] = 'Forum';
$txt['dream_portal'] = 'Dream Portal';
$txt['dp_core_modules'] = 'Collapse or Expand this Module';
$txt['dp_who_forum'] = 'Viewing the forum index of <a href="' . $scripturl . '?action=forum">' . $context['forum_name'] . '</a>.';
$txt['dp_who_portal'] = 'Viewing the portal index of <a href="' . $scripturl . '">' . $context['forum_name'] . '</a>.';
$txt['dp_who_page'] = 'Viewing the page "<a href="' . $scripturl . '?page=%1$s">%2$s</a>".';
?>
And here's the REGEX that I'm using currently that does what I need:
$data = file_get_contents('./'.$language.'/'.$file.'.'.$language.'.php');
$codes = array (
'/(\' \. \$)(.+?)( \. \')/',
'/(\= \$)(.+?)( \. \')/',
'/(\' \. \$)(.+?)(\;)/',
'/(\[\')(.+?)(\'\])/',
'/<\?php/s', '/\?>/s', '/<\?/s'
);
$html = array (
'{$2}',
'= \'{$2}',
'{$2}\';',
'[$2]',
'',
);
// Since we don't have the values for the vars.
$data = preg_replace($codes, $html, $data);
HOWEVER, since these strings are constantly changing, it may happen that a string like this is needed/inserted into any of the $txt array variables:
$txt['dp_who_page'] = 'Viewing the page "<a href="'.$scripturl.'?page=%1$s">%2$s</a>" . ';
Where the variable is bunched up with the .
and the end of the string variable has this . ';
which is different from what it was stated above, which was this:
$txt['dp_who_page'] = 'Viewing the page "<a href="' . $scripturl . '?page=%1$s">%2$s</a>".';
In either of those cases, I need it to return the above $txt['dp_who_page'] like so:
Viewing the forum index of {context[forum_name]}.
Currently it doesn't work right with the 2nd case.
How can I do this using the following preg_replace code above for both cases listed above? What needs to be changed?
I am using the eval()
php function to extract the variables.
OK, here is the entire function:
function loadLanguageFile($language, $file) {
$temp = array();
$data = file_get_contents('./'.$language.'/'.$file.'.'.$language.'.php');
$codes = array (
'/(\' \. \$)(.+?)( \. \')/',
'/(\= \$)(.+?)( \. \')/',
'/(\' \. \$)(.+?)(\;)/',
'/(\[\')(.+?)(\'\])/',
'/<\?php/s', '/\?>/s', '/<\?/s'
);
$html = array (
'{$2}',
'= \'{$2}',
'{$2}\';',
'[$2]',
'',
);
// Since we don't have the values for the vars.
$data = preg_replace($codes, $html, $data);
// We must change this because they are global.
$data = str_replace('$txt', '$langEditor_txt', $data);
$data = str_replace('$helptxt', '$langEditor_helptxt', $data);
eval($data);
if (isset($langEditor_txt)) {
$temp['txt'] = $langEditor_txt;
unset($GLOBALS['langEditor_txt']);
}
if (isset($langEditor_helptxt)) {
$temp['helptxt'] = $langEditor_helptxt;
unset($GLOBALS['langEditor_helptxt']);
}
return $temp;
}
You can go here to see what I am trying to accomplish, basically, I want to allow strings to be translated into languages easier, based on the English strings of these languages: Language TEST using this function to load the language. Click on a language (currently only spanish, french, english available), than edit the language and you will see the preg_replace take affect. Also, if you edit the DreamPortal language, it is a good example of what it is supposed to do. Also, the Default text textarea boxes are (after you click on a language View
link, than click on the Create It
link) are loading up the english strings from the actual english files in all languages. So the preg_replace is using this to input that specific string definition into the Default text textarea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议使用
gettext()
。这样您就可以使用 gettext 目录文件,这些文件可以使用 Poedit (使用 翻译记忆库)。I recommend to use
gettext()
. This way you can use gettext catalog files, which are editable with tools like Poedit (which uses translation memory as well).我不明白你为什么要这样做,但眼前的问题似乎是那些时隐时现的空间。直接的解决方案是将空格设为可选:
\s*
匹配零个或多个空白字符。I don't understand why you're doing this, but the immediate problem seems to be those here-and-gone-again spaces. The immediate solution would be to make the spaces optional:
\s*
matches zero or more whitespace characters.如果我是你,我会使用已经建议的原生
gettext
功能或 Zend_Translate 或其他 i18n 库。If i were you i would use the native
gettext
features as already suggested or Zend_Translate or another i18n library.