Reputation: 259
How would I fix lines like the last one to make them look like the first two with regex?
"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
I'm expecting
"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
Upvotes: 1
Views: 42
Reputation: 174826
Just try the below regex and replace the matched newline characters with a space.
\n(?:(?!")|(?=" +\d+))
Code:
<?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);
?>
Output:
"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
Upvotes: 3
Reputation: 67988
(?=[^"]*")\n
You can use this.replace by a space
.It uses a postive lookahead to find \n
before "
See demo.
http://regex101.com/r/hQ1rP0/14
Upvotes: 0