将.string文件格式转换为php数组格式

将.string文件格式转换为php数组格式,php,regex,arrays,Php,Regex,Arrays,希望你正在做文件 这是我的问题,我有一个用于翻译的xyz.string文件。请在下面找到文件的一小部分 /* name for an item that is duplicated in the UI, based on the original name */ "%@ (Copy)" = "%@ (kopi)"; /* display name for a book page template that is the first page of that section */ "%@ (Fi

希望你正在做文件

这是我的问题,我有一个用于翻译的xyz.string文件。请在下面找到文件的一小部分

/* name for an item that is duplicated in the UI, based on the original name */
"%@ (Copy)" = "%@ (kopi)";

/* display name for a book page template that is the first page of that section */
"%@ (First)" = "%@ (Første)";

/* display name for a book page template that represents a hardcover cover. the second argument is the cover type. */
"%@ (Hardcover, %@)" = "%1$@ (Hard innbinding, %2$@)";

/* display name for a book page template that represents a softcover cover. the second argument is the cover type. */
"%@ (Softcover, %@)" = "%1$@ (myk innbinding, %2$@)";
我想在php数组中转换翻译,如

array(
array{
"%@ (First)"=>"%@ (Første)"
},
array
{
"@ (Hardcover, %@)"=>"%1$@ (Hard innbinding, %2$@)"
}
)
等等。指定的格式不是强制性的,但应该是我可以解决的

以下是文件格式的规范

  • 键值对用相等字符(=)分隔,并且 以分号(;)结尾

  • 键和值被双引号(“”)包围

  • 占位符外观可以是:%.2f,%d,%1$s(用于 占位符:/%[\d |.]\$\d*[dsf]{1}\b+/)

  • 注释从行的开头开始,并跨越整行 或多行

  • 单行注释以双斜杠(//)多行开头 注释附在/**/

  • 除非有注释,否则注释将分配给下一个键值对 中间空白行

我知道这可以通过PREG_MATCH_ALL实现,但我无法创建一个好的正则表达式

下面是我的代码

$str=file_get_contents($_FILES['string']['tmp_name']);
preg_match_all("|(\".+\"\;)|s",preg_quote($str),$match);
echo "<pre/>";print_r($match);die;
如果有人能在这方面帮助我,我将不胜感激

谢谢
Ryan

您可以读取wohle字符串,然后使用:

上帝,雷格克斯:

"([^"]+)"\s*=\s*"([^"]+)";
解释

"([^"]+)"     # Every thing between double quotes
\s*=\s*       # Equal sign preceded or followed by any number of spaces
"([^"]+)";    # Again every thing between double quotes that ends to a ;
PHP代码:

$text = file_get_contents($_FILES['string']['tmp_name']);
preg_match_all('#"([^"]+)"\s*=\s*"([^"]+)";#', $text, $match);
$translate = array_combine($match[1], $match[2]);
print_r($translate);
示例文本的输出将是:

Array
(
    [%@ (Copy)] => %@ (kopi)
    [%@ (First)] => %@ (Første)
    [%@ (Hardcover, %@)] => %1$@ (Hard innbinding, %2$@)
    [%@ (Softcover, %@)] => %1$@ (myk innbinding, %2$@)
)

explode不会像顶部的注释那样工作;注释也会出现在数组中,好吧!如何:explode(“;”。“\n”,$string)?不,我得到的str没有新行,而且它也不可靠。我认为它会工作,但它不工作。我在匹配数组中没有得到任何东西。请在我的nxt comment/*名称中找到我在UI中重复的项目的文件内容后得到的确切字符串,基于原始名称/”%@(复制)“=”%@(kopi)”;/作为该节第一页的书页模板的显示名称/“%@(first)”=“%@(Første)”;/表示精装书的书页模板的显示名称。第二个参数是封面类型。*/“%@(精装书,%@)”=%1$@(硬装订,%2$@)“是的,它处理字符串,但不处理文件内容”奇怪“我认为在链接到.strings文件之后,它是一个文件编码问题。如果你能看一下,那就太好了。
$text = file_get_contents($_FILES['string']['tmp_name']);
preg_match_all('#"([^"]+)"\s*=\s*"([^"]+)";#', $text, $match);
$translate = array_combine($match[1], $match[2]);
print_r($translate);
Array
(
    [%@ (Copy)] => %@ (kopi)
    [%@ (First)] => %@ (Første)
    [%@ (Hardcover, %@)] => %1$@ (Hard innbinding, %2$@)
    [%@ (Softcover, %@)] => %1$@ (myk innbinding, %2$@)
)