Jump to content

Archived

This topic is now archived and is closed to further replies.

redarrow

valadation problam concept cheers.

Recommended Posts

This example the foreach does only ouput the > correctly but when i use in the $mess < get nothink.
[code]
<?php
$mess="hi there < hows you";

$not_allowed=array("<",">");

foreach($not_allowed AS $key => $val){

if(eregi($key,$mess)){

echo "word bad";
exit;

}else{

echo "word ok";
exit;
}
}
?>
[/code]

This code i get nothink on the screen.
[code]
<?php
$mess="hi there < hows you";

$not_allowed=array("<",">");

for($i=0; $i<count($not_allowed); $i++){

if(eregi($key,$mess)){

echo "word bad";
exit;

}else{

echo "word ok";
exit;
}
}
?>
[/code]

Share this post


Link to post
Share on other sites
For your first code snipped you'll wnat to use $val rather than $key in your if statement so the code should be this:
[code=php:0]<?php

$mess = "hi there < hows you";

$not_allowed = array("<", ">");

foreach($not_allowed as $val)
{
    if(eregi($val, $mess))
    {
        echo "word bad";
        exit;
    }
    else
    {
        echo "word ok";
        exit;
    }
}

?>[/code]
However havn't we goine over this subject many times before? I can recall you posting something similar to this before

Share this post


Link to post
Share on other sites
i am getting words ok why?

thank you for your help cheers i no we went over this before but im getting strange results.

[code]
<?php

$mess = "hi there > hows you";

$not_allowed = array("<", ">");

foreach($not_allowed as $val)
{
    if(eregi($val, $mess))
    {
        echo "word bad";
        exit;
    }
    else
    {
        echo "word ok";
        exit;
    }
}

?>
[/code]

Share this post


Link to post
Share on other sites
Because you are using exit on the if/else statement. When you use exit is stops the script from running. So if < was not found in the string the script would exit and print word ok. However if > was found it'll exit and show word bad. If you remove the exit; it'll print word okword bad.

You dont to use exit in the if/else statement otherwise it wont complete the loop. It'll only run once.

Prehaps you want to do something like this:
[code=php:0]$mess = "hi there > hows you";

$not_allowed = array("<", ">");

foreach($not_allowed as $val)
{
    if(eregi($val, $mess))
    {
        echo "We have detected a word/symbol(s) that we do not accept. Please do not use any of the words/symbols below:<br />\n";

        echo 'Words/Sybols not allowed: ' . implode(", ", $not_allowed);
    }
}[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.