Solved

preg_match no match for specific text

Posted on 2006-11-24
8
271 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I fix this UPDATE error? 7 32
Currency in SQL? 2 32
php help 34 58
Undefined variable problem 5 23
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

830 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