Solved

Multiple preg_match regex on title or description

Posted on 2014-12-22
3
138 Views
Last Modified: 2014-12-22
Google has several restricted items.  I need to use regex to detect these items and flag them so they are not entered in the google shopping feed.  I can do one, I just cannot get several to work together.

example:
$a = 'Do you have any guns, knives, scopes, magazines,?';

if (preg_match('/guns/','/knives/,/scopes/,/magazine/,$a))  
    echo 'true';

Open in new window

0
Comment
Question by:lawrence_dev
3 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 250 total points
ID: 40513991
1. Use the Regex Coach to build out your regular expressions. It's a free utility and you can see your results instantly as you type. I know regexes pretty well and still use it all the time.

2. You can group matching options like this:
(?:apples|oranges|bananas)

That will match apples OR oranges OR bananas.

if (preg_match('/(?:guns|knives|scopes|magazine)/,$a))

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 40514151
You'll want to include word boundaries ( \b ) in gr8gonzo's suggestion. Otherwise, things like "shoguns" and "periscopes" will be matched as well--unless that's the behavior you want.

e.g.

if (preg_match('/\b(?:guns|knives|scopes|magazine)\b/,$a))

Open in new window

0
 

Author Closing Comment

by:lawrence_dev
ID: 40514304
Thank You!!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
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 …

790 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