Forum Moderators: coopster

Message Too Old, No Replies

not operator with eregi

         

littlegiant

4:47 pm on Apr 12, 2006 (gmt 0)

10+ Year Member



I'm new to PHP so humble apologies if this is a dumb question but can you use the NOT (!) operator with the eregi function? I've tried this and I'm getting some unexpected results.

Example:

$var1 = "test1;";
$var2 = "test2";
$var3 = "test3";
$var4 = "test4";

$variables = array ($var1,$var2,$var3,$var4);

for ($i = 0; $i < 4; $i++) {
if (!eregi('1', $variables[$i])) die("ACCESS DENIED.");
}

echo "okay";

I would like a regular expression to test for no match rather than a match. Here I'm using a for loop to cycle through some variables and test for no match for the literal '1'. Yet this script keeps outputting 'ACCESS DENIED' and I don't get it. The first variable contains a '1' in it, hence a match is found and the script should echo 'okay'.

The following script works as I expect it to. It's the exact same thing except it tests for no match for 'test'. Since a match is found, it prints 'okay'.

$var1 = "test1;";
$var2 = "test2";
$var3 = "test3";
$var4 = "test4";

$variables = array ($var1,$var2,$var3,$var4);

for ($i = 0; $i < 4; $i++) {
if (!eregi('test', $variables[$i])) die("ACCESS DENIED.");
}

echo "okay";

I suspect that this is due to some simple blunder I've made. I would greatly appreciate if someone would point it out.

Much thanks.

littlegiant

6:00 pm on Apr 12, 2006 (gmt 0)

10+ Year Member



I'm scouring the internet looking for an answer to this simple question and I can't come up with any references to using the NOT operator with the eregi() function and yet it seems like it's possible. I just need a simple answer to this question. You don't even have to look at my example code I previously posted. If it's possible, I'll take it from there and debug the code myself. I just want to know if it's possible.

YES or NO?

Anybody?

jatar_k

6:04 pm on Apr 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



yes you can

I've used this for testing email addresses

if (!ereg($pattern, $EMAIL) && $EMAIL!= "") {
$errmsg .= "<br>Please enter a valid email address";
}

which uses ereg but it should be the same

feel better? ;)

littlegiant

6:15 pm on Apr 12, 2006 (gmt 0)

10+ Year Member



Yes thank you. Humble apologies. I'm not usually so impatient. It's just that I actually own two books on PHP and I've downloaded the official PHP manual (parts of which I'm sure were not meant for human consumption) and I was becoming a little exasperated that I couldn't come up with the answer to this on my own.

jatar_k

6:23 pm on Apr 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



no worries, happens when something seems so simple and you can't find it

hence my wink, been there myself more times than I care to admit ;)

littlegiant

7:27 pm on Apr 12, 2006 (gmt 0)

10+ Year Member



Okay I figured it out (he's a learner!)...

In my original script, I replaced the literal '1' with the class '[0-9]' and now the script is performing as I expect it to. Not sure why the '1' malfunctions. According to my information, a literal can be any character (albeit certain characters have to be escaped using the '\'). As I said, still learning... :o)