2014-01-13 190 views
0

希望你在做文件轉換.string文件格式轉化成PHP數組格式

因此,這裏是我的問題,我有一個用於translation.Please一個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, %@)" = "%[email protected] (Hard innbinding, %[email protected])"; 

/* display name for a book page template that represents a softcover cover. the second argument is the cover type. */ 
"%@ (Softcover, %@)" = "%[email protected] (myk innbinding, %[email protected])"; 

我想翻譯即轉換成一個PHP數組一樣

array(
array{ 
"%@ (First)"=>"%@ (Første)" 
}, 
array 
{ 
"@ (Hardcover, %@)"=>"%[email protected] (Hard innbinding, %[email protected])" 
} 
) 

等被指定on.The格式不是強制性的,但它應該是,我可以解決方法。

以下是文件格式

  • 鍵 - 值對被分隔與等於字符(=)的以分號結束的說明書,和 (;)。

  • 鍵和值由雙引號(「)包圍

  • 佔位符的外觀可以是:%.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, %@)" = "%[email protected] (Hard innbinding, %[email protected])"; 

如果有人能幫助我在這一點,我們將不勝感激

感謝 瑞安

回答

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, %@)] => %[email protected] (Hard innbinding, %[email protected]) 
    [%@ (Softcover, %@)] => %[email protected] (myk innbinding, %[email protected]) 
) 
+0

我認爲它會工作,但它的工作原理不工作我沒有得到任何東西在匹配數組中,請找到確切的字符串,我得到的文件file_get_content在我的nxt註釋 –

+0

/*名稱爲基於原始名稱重複在UI中的項目* /「%@ (複製)「=」%@(kopi)「;/*顯示該部分第一頁的書頁模板的名稱* /」%@(First)「=」%@(Første)「;/*顯示書名頁面模板的名稱,表示精裝封面,第二個參數是封面類型。* /「%@(Hardcover,%@)」=「%1 $ @(堅固的建築,%2 $ @)」; –

+0

@Ryandecosta現場演示:http://3v4l.org/OsVQd – revo

0

你可以只讀取wohle字符串,然後使用:explode

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

爆炸將無法正常工作,在頂部的評論可以有;並且意見也將在陣列中出現 –

+0

哦,好的! 怎麼樣:爆炸('「;'。」「\ n」,$字符串)? – Yami

+0

不行,我得到的str沒有新行,而且它也不可靠 –