How to remove boolean operators from keywords in php

I found this line of code:
preg_replace("/^[^a-z0-9]?(.*?)[^a-z0-9]?$/i", "$1", $word);

How can I change it to strictly remove only boolean operators that occur at the beginning or at the end of the $word.

These are the boolean operators to remove:
+ - > < ( ) ~ * '(single quotes) "(double quotes)

LVL 1
aristanobleAsked:
Who is Participating?
 
Roger BaklundCommented:
It does not accept any character outside the range a-z or 0-9 as the first or last character.

You can replace both occurences of [^a-z0-9] with [+\\-><\\(\\)~*'\"] to remove only the characters you list.
$word = preg_replace("/^[+\\-><\\(\\)~*'\"]?(.*?)[+\\-><\\(\\)~*'\"]?$/i", "$1", $word);

Open in new window

0
 
Roger BaklundCommented:
It seems to be correct... but you must fetch the result:
$word = preg_replace("/^[^a-z0-9]?(.*?)[^a-z0-9]?$/i", "$1", $word);

Open in new window

0
 
aristanobleAuthor Commented:
I'm not that good at regular expressions but would this only remove the characters as intended and no others? And only from the beginning and end of the word without any in between?
0
 
aristanobleAuthor Commented:
Awesome! Thank Cxr!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.