我如何修复最后一行,使它们看起来像前两个正则表达式?
"XY30-0601" "LMF II Trump" 7
"ZR3-0003601" "Durable canvas" 3
"GQ450-020061" "DMZ Power
Epic
Durable canvas
Handle for easy transport
Sturdy buttons to keep the case rolled up
Reinforced case stitching
" 17
我期待
"XY30-0601" "LMF II Trump" 7
"ZR3-0003601" "Durable canvas" 3
"GQ450-020061" "DMZ Power Epic Durable canvas Handle for easy transport Sturdy buttons to keep the case rolled up Reinforced case stitching" 17
答案 0 :(得分:3)
试试下面的正则表达式,并用空格替换匹配的换行符。
\n(?:(?!")|(?=" +\d+))
<强>代码:强>
<?php
$string = <<<EOT
"XY30-0601" "LMF II Trump" 7
"ZR3-0003601" "Durable canvas" 3
"GQ450-020061" "DMZ Power
Epic
Durable canvas
Handle for easy transport
Sturdy buttons to keep the case rolled up
Reinforced case stitching
" 17
EOT;
$pattern = '~\n(?:(?!")|(?=" +\d+))~';
$replacement = " ";
echo preg_replace($pattern, $replacement, $string);
?>
<强>输出:强>
"XY30-0601" "LMF II Trump" 7
"ZR3-0003601" "Durable canvas" 3
"GQ450-020061" "DMZ Power Epic Durable canvas Handle for easy transport Sturdy buttons to keep the case rolled up Reinforced case stitching " 17
答案 1 :(得分:0)