将两个preg_replace语句合并为一个

问题描述 投票:0回答:1

我尝试将两个preg_replace语句合并为一个。

第一条陈述:

$string = preg_replace("/\/\\*\\*\//","",$string); // to remove any "/**/" frome the string.

第二条语句:

$string = preg_replace("/\/\//","",$string); // to remove any "//" from the string.

我的目标是:

1:删除所有出现的“ / ** /”,而消除它们之间的任何字符。例如“ / * // * /”不应删除。

2:删除不属于“ / ** /”的所有“ //”。

例如由于步骤1,“ // ** /”应该离开第一个“ /”,但是在“ / * // * /”的情况下,不应删除两个“ *”之间的“ //”。

3:在一个preg_replace语句中获得步骤1和2。

字符串中的剩余字符应为与步骤1或步骤2中的条件不匹配的任何“ *”或“ /”。

预先感谢。

php preg-replace
1个回答
0
投票

使用交替:

$string = preg_replace("/\/\\*\\*\/|\/\//", "", $string);
© www.soinside.com 2019 - 2024. All rights reserved.