Solved

preg_match no match for specific text

Posted on 2006-11-24
8
268 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why isnt it sending mail from my php but is from my server 10 35
PHP7 and Sql Server Windows 2008 R2 13 85
Calculating number of days between two dates 7 31
html input type 3 21
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.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 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

20 Experts available now in Live!

Get 1:1 Help Now