我想搜索并替换PO file (its a translation file for CakePHP)
中特定字符#id:
后的字符串
这是PO文件中的示例文本。
# id: 4
msgctxt "team 1"
msgid "john"
msgstr "전세계 강사와 영어회화 레슨을 받을 수 있는 서비스입니다."
# id: 5
msgctxt "team 2"
msgid "jane"
msgstr "translation here"
# id: 6
msgctxt "team 3"
msgid "jen"
msgstr "translation here"
..
and so on..
例如,我想替换数据库中#id: 5
后面的字符串,它包含msgctxt, msgid, msgstr
。
I got this column in my database msgid, msgctxt and msgstr
预期输出如下所示:
# id: 4
msgctxt "team 1"
msgid "john"
msgstr "전세계 강사와 영어회화 레슨을 받을 수 있는 서비스입니다."
# id: 5
msgctxt "team 2"
msgid "jane"
msgstr "translation here"
# id: 6
msgctxt "team 3"
msgid "jen"
msgstr "translation here"
..
and so on..
我试过这个,但是我不知道如何在PO文件中使用str_replace
找到字符串。
$newContent .= '#id: ' . $po['id'] . "\n";
$newContent .= 'msgctxt "' . trim($po['msgctxt']) . '"' . "\n";
$newContent .= 'msgid "' . trim($po['msgid']) . '"' . "\n";
$newContent .= 'msgstr "' . trim($po['msgstr']) . '"';
$file = file_get_contents($filepath);
$data = str_replace($file, $newContent, $file); // I dont think if its correct to find the string.
file_put_contents($filepath, $data);
请注意:即,#id:
是动态的。
更新:有时候,我会在PO文件中得到这些数据。
# id: 4
# : https://url // sometimes don't have this
msgctxt "team 1" // sometimes don't have this
msgid "john"
msgstr "translation here"
# id: 5
msgid "jane"
msgstr "translation here"
# id: 6
msgctxt "team 3"
msgid "jen"
msgstr "translation here"
1条答案
按热度按时间2ic8powd1#
我想你可以试试这个
$1
、$2
和$3
是指msgctxt
、msgid
和msgstr
背后的原始内容,您可以将其更改为任何您喜欢的信息。您也可以将[0-9]+
更改为任何您想要的特定id。