开发者

preg_replace every except: spaces, (, ), numbers, AND, OR

开发者 https://www.devze.com 2023-03-26 23:29 出处:网络
I want to replace everything except the following: spaces ( ) numbers AND OR Here\'s my current code which almost works except it allows any of the letters from ANDOR to be left.

I want to replace everything except the following:

spaces
(
)
numbers
AND
OR

Here's my current code which almost works except it allows any of the letters from ANDOR to be left.

I want it to only leave the full word AND or O开发者_开发百科R otherwise strip it.

$string = preg_replace("/[^()ANDOR0-9 ]/", "", $string);

Any ideas what code I need?


$a1 = array("AND", "OR");
$a2 = array("&","|");
$result = str_replace($a2, $a1, preg_replace("/[^()0-9 &|]/", "", str_replace($a1, $a2, preg_replace("/[^()ANDOR0-9 ]/", "", $string))));

Not very elegant, but it works.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号