Solved

preg_match no match for specific text

Posted on 2006-11-24
8
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
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 look for a specific file type in a local or remote server directory using PHP.

631 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