Solved

Multiple preg_match regex on title or description

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=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…
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.
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 count occurrences of each item in an array.

743 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

13 Experts available now in Live!

Get 1:1 Help Now