Solved

Multiple preg_match regex on title or description

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
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…
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…

831 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