Though not explicitly explained in the question, the goal of this task is to interrogate the pipe-enclosed substrings. If the substring has more than one double-quoted substring in it, then the pipes must be preserved. If there is only one double-quoted substring inside of the pipes, then these pipes should be removed. The question speaks about checking for commas, but this can lead to incorrect results if one of the double-quoted substrings contains a comma. The same kind of monkeywrenching scenario may occur if an escaped double-quote exists within the double-quoted substring.
Don't understand what I am going on about? I can understand that, so I have a demonstration and a new regex pattern that draws upon the wisdom of this post.
Code: (Demo)
$strings = [
'(STR,0:30) + |"Outdoor"| + |"Chair Mat","Commercial Floor Mat","Door Mat"| + [Post Type] + (STR,0:30)',
'(STR,0:30) + |"Outdoor"| + |"Red, Yellow, and Green Jamaican Mat"| + [Post Type] + (STR,0:30)',
'(STR,0:30) + |"Outdoor"| + |"USA \"MAGA\" Mat"| + [Post Type] + (STR,0:30)',
];
print_r(
preg_replace('~\|("[^"\\\\]*(?:\\\\"[^"\\\\]*)*")\|~', '\1', $strings)
);
Output:
Array
(
[0] => (STR,0:30) + "Outdoor" + |"Chair Mat","Commercial Floor Mat","Door Mat"| + [Post Type] + (STR,0:30)
[1] => (STR,0:30) + "Outdoor" + "Red, Yellow, and Green Jamaican Mat" + [Post Type] + (STR,0:30)
[2] => (STR,0:30) + "Outdoor" + "USA \"MAGA\" Mat" + [Post Type] + (STR,0:30)
)
Notice how "Chair Mat","Commercial Floor Mat","Door Mat"
is correctly treated as multiple entries. Conversely "Red, Yellow, and Green Jamaican Mat"
and "USA \"MAGA\" Mat"
are single entries, so they appropriately have their pipes removed.
preg_replace('~\|"([^",]+)"\|~', '"$1"', $string)
, see this regex demo. – Wiktor Stribiżew Jun 30 '20 at 22:22||
? Confused. Are the+
signs apart of the string? Perhaps if you could include the source data in a more "raw" format in your question. – GetSet Jun 30 '20 at 22:26preg_replace('/\|"([a-z ]+)"\|/i', '"$1"', $string);
– The fourth bird Jul 1 '20 at 8:44