Solved

preg_match no match for specific text

Posted on 2006-11-24
8
266 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
Comment Utility
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
Comment Utility
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
Comment Utility
How about this then:

<?php

$expression = 'ab';

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

?>
0
 
LVL 1

Author Comment

by:vacerose
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 29

Accepted Solution

by:
TeRReF earned 500 total points
Comment Utility
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
Comment Utility
Thanks for trying.  I guess I'll have to go ahead and tweak the code.
0
 
LVL 29

Expert Comment

by:TeRReF
Comment Utility
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
Comment Utility
Anyway, thanks for the points/ grade and good luck.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to count occurrences of each item in an array.

772 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

11 Experts available now in Live!

Get 1:1 Help Now