\n
我正在尝试转换一个多行字符串,以在两个最后的右括号连续出现后插入换行符,即:))
变为))\n
.
也可能在 '))' 之前有一个 ')',有效地创建了 ')))'。
这两个或三个括号可能会或可能不会被不确定长度的空格“展开”,例如))
, ) )
, )))
, ) ) )
,)) )
等等) ))
。
我尝试了以下方法:
//Example message
$message = '(item (name 286) (Index 31) (Image "item001") (class money coin) (code 4 110 0) (country 2) (plural 1) (buy 0))
(item(name 7904)(Index 7904) (specialty (Dex 10(defense 55)(hp 3500)(dodge 71) ))
(item(name 7905)(Index 7905)(country 2)
(level 80)(specialty(hp 3400) ) )
(item(name 7906)(Index 7906)(level 80) (specialty(Str 10)) ) ';
// Converts all lines into one line
$message = preg_replace("/[\r\n]*/","",$message);
// Replace '))' with '))\n' - doesn't work.
$message = preg_replace("/[)s+)]s*/","\n",$message);
$InititemLines = explode("\n", $message);
for ($line = 0; $line < count($InititemLines); $line++) {
echo "Line #<b>{$line}</b> : " . $InititemLines[$line] . "<br />\n";
}
要将所有行转换为一行,我使用了:
$message = preg_replace("/[\r\n]*/","",$message);
然后,要替换))
为))\n
,我尝试了以下方法(但它不起作用):
$message = preg_replace("/[)s+)]s*/","))\n",$message);
我希望输出是这样的:
Line #0: (item (name 286) (Index 31) (Image "item001") (class money coin) (code 4 11 0 0) (country 2) (plural 1) (buy 0))
Line #1: (item(name 7904)(Index 7904) (specialty (Dex 10)(defense 55)(hp 3500)(dodge 71) ))
Line #2: (item(name 7905)(Index 7905)(country 2)(level 80)(specialty(hp 3400) ) )
Line #3: (item(name 7906)(Index 7906)(level 80) (specialty(Str 10)) )