简体   繁体   English

删除特殊字符“ | ” 从字符串中忽略,如果它在开始 |“ End ”| 之间包含“,”

[英]Remove special character “ | ” from a string ignore if it contains “,” between starts |“ End ”|

String:细绳:

(STR,0:30) + |"Outdoor"| (STR,0:30) + |“户外”| + |"Chair Mat","Commercial Floor Mat","Door Mat"| + |"椅子垫","商用地垫","门垫"| + [Post Type] + (STR,0:30) + [帖子类型] + (STR,0:30)

Result:结果:

(STR,0:30) + "Out door" + |"Chair Mat","Commercial Floor Mat","Door Mat"| (STR,0:30) + "户外" + |"椅子垫","商用地垫","门垫"| + [Post Type] + (STR,0:30) + [帖子类型] + (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 ""

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)

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.如果 substring 中有多个双引号 substring,则必须保留管道。 If there is only one double-quoted substring inside of the pipes, then these pipes should be removed.如果管道内只有一个双引号 substring,则应移除这些管道。 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.如果双引号 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ 中存在转义的双引号,则可能会发生同样类型的猴子痛苦场景。

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: 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.请注意"Chair Mat","Commercial Floor Mat","Door Mat"如何被正确地视为多个条目。 Conversely "Red, Yellow, and Green Jamaican Mat" and "USA \"MAGA\" Mat" are single entries, so they appropriately have their pipes removed.相反"Red, Yellow, and Green Jamaican Mat""USA \"MAGA\" Mat"是单个条目,因此它们适当地移除了管道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM