Solved

preg_match no match for specific text

Posted on 2006-11-24
8
270 Views
Last Modified: 2008-03-10
I need a regular expression for preg_match that will only match if the whole text isn't a specific value.
For example I want to exclude 'abc'.

so if the input is 'abcd' or 'ab' then it will return all the text 'abcd' or 'ab'. But if the input is 'abc' it should return nothing.
0
Comment
Question by:vacerose
  • 5
  • 3
8 Comments
 
LVL 29

Expert Comment

by:TeRReF
ID: 18008831
How about:

<?php

function getValidString($s, $valid) {
        if ((preg_match('/^'.$valid.'$/is', $s, $match)) === 1)
                return false;
        return true;
}

$string = 'abc';
$string2 = 'ab';

if (getValidString($string, 'abc')) {
        echo $string;
}

if (getValidString($string2, 'abc')) {
        echo $string2;
}


?>
0
 
LVL 1

Author Comment

by:vacerose
ID: 18009026
That's the right idea but I need to get it done with a call only to preg_match without using a helper function to invert the return value.

the structure would be along these lines

<?php

$expression = 'a';

if (preg_match('/'.$expression.'/', 'abc'))
      echo 'good';
else
      echo 'bad';

?>

essentialy I need an expression that is true for any string except 'abc'
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 18009079
How about this then:

<?php

$expression = 'ab';

if ((preg_match('/^'.$expression.'$/is', 'abc')) === 0)
     echo 'good';
else
     echo 'bad';
print_r($match);

?>
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 1

Author Comment

by:vacerose
ID: 18009120
That is very close but is there a way to handle the negation in the expression rather than adding "=== 0" ?

I not trying to be a pain, I'm just trying to make a user configurable expression setting work without modifying some existing code.

Thanks.
0
 
LVL 29

Accepted Solution

by:
TeRReF earned 500 total points
ID: 18009162
No, that's not possible, you either use my previous comment, of you do this (flip good and bad):

<?php

$expression = 'ab';

if ((preg_match('/^'.$expression.'$/is', 'abc')))
     echo 'bad';
else
     echo 'good';

?>
0
 
LVL 1

Author Comment

by:vacerose
ID: 18009188
Thanks for trying.  I guess I'll have to go ahead and tweak the code.
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 18009212
Well.... there is another way if you drop the preg_match... you could make it a simple string comparison:
if ($expression === $string)
     echo 'bad';
else
     echo 'good';
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 18009215
Anyway, thanks for the points/ grade and good luck.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Testing a string condition in PHP 6 20
hasing a url 16 24
preventing spam with mailto 22 29
Checkout Page Input Field not aligned 1 20
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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.

813 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

18 Experts available now in Live!

Get 1:1 Help Now