?
Solved

How to remove boolean operators from keywords in php

Posted on 2009-02-17
4
Medium Priority
?
306 Views
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)

0
Comment
Question by:aristanoble
  • 2
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 23665140
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
 
LVL 1

Author Comment

by:aristanoble
ID: 23667452
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
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 200 total points
ID: 23669815
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
 
LVL 1

Author Closing Comment

by:aristanoble
ID: 31548038
Awesome! Thank Cxr!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

839 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