0

String:

(STR,0:30) + |"Outdoor"| + |"Chair Mat","Commercial Floor Mat","Door Mat"| + [Post Type] + (STR,0:30)

Result:

(STR,0:30) + "Out door" + |"Chair Mat","Commercial Floor Mat","Door Mat"| + [Post Type] + (STR,0:30)

I have tried

preg_replace('/\|\"[a-z A-Z]\"\|/i', '"', $string);

I am not able to find any solution the only condition I know is that the string will start with |" and End with "| and if it does not contains "," between we can remove or replace |" and "| with "

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    Try `preg_replace('~\|"([^",]+)"\|~', '"$1"', $string)`, see [this regex demo](https://regex101.com/r/sSfJeO/1). – Wiktor Stribiżew Jun 30 '20 at 22:22
  • Do you want to remove double `||`? 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:26
  • See [this](https://meta.stackexchange.com/questions/51144/how-do-i-post-code-in-stackoverflow/51145#:~:text=6%20Answers&text=If%20you%20post%20code%20or,format%20and%20syntax%20highlight%20it.&text=Or%20for%20block%20code%2C%20you,%22it%20works!%22) about formatting code and data in your text. – Cary Swoveland Jun 30 '20 at 23:06
  • Using the character class `preg_replace('/\|"([a-z ]+)"\|/i', '"$1"', $string);` – The fourth bird Jul 01 '20 at 08:44
  • 1
    @WiktorStribiżew Please stop commenting solutions -- this is not what comments are for and you know this. When you do it, users with less rep than you mimic your behavior. This puts content in inappropriate locations on the page. Please set a better example. – mickmackusa Jul 01 '20 at 14:19

2 Answers2

0

I think you can use a regex like this:

\|("[^,]+")\|

Regex demo

$re = '/\|("[^,]+")\|/m';
$str = '(STR,0:30) + |"Outdoor"| + |"Chair Mat","Commercial Floor Mat","Door Mat"| + [Post Type] + (STR,0:30)';
$subst = '$1';

$result = preg_replace($re, $subst, $str);

echo "The result of the substitution is ".$result;
// (STR,0:30) + "Outdoor" + |"Chair Mat","Commercial Floor Mat","Door Mat"| + [Post Type] + (STR,0:30)
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
0

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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136