从字符串中删除特殊字符“|”如果它在开始之间包含“,”|“结束”|则忽略
Remove special character " | " from a string ignore if it contains "," between starts |" End "|
字符串:
(STR,0:30) + |"Outdoor"| + |"Chair Mat","Commercial Floor Mat","Door Mat"| + [Post Type] + (STR,0:30)
结果:
(STR,0:30) + "Out door" + |"Chair Mat","Commercial Floor Mat","Door Mat"| + [Post Type] + (STR,0:30)
我试过了
preg_replace('/\|\"[a-z A-Z]\"\|/i', '"', $string);
我找不到任何解决方案,我知道的唯一条件是字符串将以 |"
开头并以 "|
结尾,如果它不包含 ","
我们可以删除或替换 |"
和 "|
为 "
我想你可以像这样使用正则表达式:
\|("[^,]+")\|
$re = '/\|("[^,]+")\|/m';
$str = '(STR,0:30) + |"Outdoor"| + |"Chair Mat","Commercial Floor Mat","Door Mat"| + [Post Type] + (STR,0:30)';
$subst = '';
$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)
虽然问题中没有明确解释,但此任务的目标是询问管道封闭的子串。 如果子字符串中有多个双引号子字符串,则管道必须保留。如果管道内只有一个双引号子字符串,则应删除这些管道。问题是关于检查逗号,但如果其中一个双引号子字符串包含一个逗号。如果双引号子字符串中存在转义双引号,则可能会出现同样的猴子扭曲场景。
不明白我在说什么?我能理解,所以我有一个演示和一个新的正则表达式模式,它利用了 the wisdom of this post.
代码:(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('~\|("[^"\\]*(?:\\"[^"\\]*)*")\|~', '', $strings)
);
输出:
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)
)
注意如何将 "Chair Mat","Commercial Floor Mat","Door Mat"
正确地视为多个条目。相反 "Red, Yellow, and Green Jamaican Mat"
和 "USA \"MAGA\" Mat"
是单个条目,因此它们适当地删除了管道。
字符串:
(STR,0:30) + |"Outdoor"| + |"Chair Mat","Commercial Floor Mat","Door Mat"| + [Post Type] + (STR,0:30)
结果:
(STR,0:30) + "Out door" + |"Chair Mat","Commercial Floor Mat","Door Mat"| + [Post Type] + (STR,0:30)
我试过了
preg_replace('/\|\"[a-z A-Z]\"\|/i', '"', $string);
我找不到任何解决方案,我知道的唯一条件是字符串将以 |"
开头并以 "|
结尾,如果它不包含 ","
我们可以删除或替换 |"
和 "|
为 "
我想你可以像这样使用正则表达式:
\|("[^,]+")\|
$re = '/\|("[^,]+")\|/m';
$str = '(STR,0:30) + |"Outdoor"| + |"Chair Mat","Commercial Floor Mat","Door Mat"| + [Post Type] + (STR,0:30)';
$subst = '';
$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)
虽然问题中没有明确解释,但此任务的目标是询问管道封闭的子串。 如果子字符串中有多个双引号子字符串,则管道必须保留。如果管道内只有一个双引号子字符串,则应删除这些管道。问题是关于检查逗号,但如果其中一个双引号子字符串包含一个逗号。如果双引号子字符串中存在转义双引号,则可能会出现同样的猴子扭曲场景。
不明白我在说什么?我能理解,所以我有一个演示和一个新的正则表达式模式,它利用了 the wisdom of this post.
代码:(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('~\|("[^"\\]*(?:\\"[^"\\]*)*")\|~', '', $strings)
);
输出:
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)
)
注意如何将 "Chair Mat","Commercial Floor Mat","Door Mat"
正确地视为多个条目。相反 "Red, Yellow, and Green Jamaican Mat"
和 "USA \"MAGA\" Mat"
是单个条目,因此它们适当地删除了管道。