转换.string文件格式转化成PHP数组格式 [英] converting .string file format into php array format
问题描述
希望你在做文件
因此,这里是我的问题,我有一个用于translation.Please一个xyz.string文件中找到以下文件的一小部分。
So here is my question i have a xyz.string file that is used for translation.Please find the a small part of file below.
/* 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数组像
i want to convert the translation i.e in a php array like
array(
array{
"%@ (First)"=>"%@ (Første)"
},
array
{
"@ (Hardcover, %@)"=>"%1$@ (Hard innbinding, %2$@)"
}
)
和所以指定盘符,格式不是强制性的,但它应该是,我可以解决方法。
and so on.The format that is specified is not mandatory but it should be something that i can workaround with.
以下是文件格式的规范
-
键 - 值对与平等字符分隔(=)和
(;)。
key-value pairs are delimited with the equal character (=), and terminated by a semicolon (;).
键和值是用双引号()
占位外观可以是:%.2f,%D,%1 $ S(普通前pression为
占位符:/%[。\\ D |]的 \\ $ 的\\ D * [DSF] {1} \\ B + /)
place-holders look can be: %.2f, %d, %1$s (regular expression for placeholders: /%[\d|.]\$\d*[dsf]{1}\b+/)
注释开始在该行的开始和跨越整条生产线
或者多行
comments start at the beginning of the line and span the whole line or multiple lines
单行注释开始用双斜杠(//)多行
评论被包围在/ * * /
single-line comments start with double slashes (//) multi-line comments are enclosed in /* */
除非有任何评论被分配给下一个键 - 值对
空行
a comment is assigned to the next key-value pair unless there are any blank lines in between
我知道这可以通过preG_MATCH_ALL可以实现,但我不能够创建一个漂亮的定期EX pression
下面是我的code
$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后,我得到确切的字符串是继
the exact string i am getting after file_get_content is following
/* 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$@)";
如果任何人都可以帮我在这这将是pciated多少AP $ P $
If anyone can help me in this it will be much appreciated
谢谢
瑞安
推荐答案
主,正则表达式:
"([^"]+)"\s*=\s*"([^"]+)";
说明的
Explanations:
"([^"]+)" # 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 code:
PHP code:
$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);
在输出作为您的示例文本将是:
The output for your sample text will be:
Array
(
[%@ (Copy)] => %@ (kopi)
[%@ (First)] => %@ (Første)
[%@ (Hardcover, %@)] => %1$@ (Hard innbinding, %2$@)
[%@ (Softcover, %@)] => %1$@ (myk innbinding, %2$@)
)
这篇关于转换.string文件格式转化成PHP数组格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!