We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


How to remove boolean operators from keywords in php

aristanoble asked
Medium Priority
Last Modified: 2013-12-12
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)

Watch Question

Top Expert 2008

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


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?
Top Expert 2008
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Awesome! Thank Cxr!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.