How to remove boolean operators from keywords in php

Posted on 2009-02-17
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)

Question by:aristanoble
    LVL 39

    Expert Comment

    by:Roger Baklund
    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

    LVL 1

    Author Comment

    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?
    LVL 39

    Accepted Solution

    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

    LVL 1

    Author Closing Comment

    Awesome! Thank Cxr!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now