Jump to content

valadation problam concept cheers.


redarrow

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]
Link to comment
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
Link to comment
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]
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.