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

时间:2014-01-13 07:43:18

标签: 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 */
"%@ (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;

在file_get_content跟随

之后我得到的确切字符串
/* 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$@)";

如果有人可以帮助我,我们将不胜感激

由于 莱恩

2 个答案:

答案 0 :(得分:1)

主,正则表达式:

"([^"]+)"\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$@)
)

答案 1 :(得分:0)

您可以阅读整个字符串,然后使用:explode

$array = explode(';', $string);