redarrow Posted September 13, 2006 Share Posted September 13, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/20661-valadation-problam-concept-cheers/ Share on other sites More sharing options...
wildteen88 Posted September 13, 2006 Share Posted September 13, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/20661-valadation-problam-concept-cheers/#findComment-91366 Share on other sites More sharing options...
redarrow Posted September 13, 2006 Author Share Posted September 13, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/20661-valadation-problam-concept-cheers/#findComment-91370 Share on other sites More sharing options...
wildteen88 Posted September 13, 2006 Share Posted September 13, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/20661-valadation-problam-concept-cheers/#findComment-91377 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.