Solved

Multiple preg_match regex on title or description

Posted on 2014-12-22
3
135 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

867 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

12 Experts available now in Live!

Get 1:1 Help Now