Forum Moderators: coopster

Message Too Old, No Replies

$variable[x]... does it work?

         

sebbothebutcher

10:37 pm on May 28, 2004 (gmt 0)

10+ Year Member



hi! it's me again with a problem... i have a function that should go throgh a string and check every of its 250 characters if there is either "<" or ">" in one or more of them... now the function looks like this:

function clean($string)
{
$c_string[250] = $string;
$x = 0;
while($x <= 250)
{
if($c_string[x] == "<" ¦¦ $c_string[x] == ">")
{
echo "Your entry/username contained illegal characters!";
exit();
}
$x++;
}
}

i've guessed this from my little knowledge of c and c++... can anybody tell me, why it doesn't work? or if it's even possible to write something like this in php? thanks in advance!

WhosAWhata

11:14 pm on May 28, 2004 (gmt 0)

10+ Year Member



why 250?
function clean($string) {
for($x=0,$x<=250;$x++){
if(($string[x] == "<") ¦¦ ($string[x] == ">")) {
echo "Your entry/username contained illegal characters!";
exit();
}
}
}

if you don't care about the legnth (250)

function clean($string) {
if ((ereg("<",$string)) ¦¦ (ereg(">",$string))) {
echo "Your entry/username contained illegal characters!";
exit();
}
}

sebbothebutcher

11:03 am on May 29, 2004 (gmt 0)

10+ Year Member



okay thanks... it's working now! the 250 length is from an older version, sorry... i've worked around that bit already! your script could help me! thanks for your help!

DevlshOne

8:06 pm on Jun 8, 2004 (gmt 0)

10+ Year Member



You could also use:

function clean($string) {
if ((strpos($string,">")>0) ¦¦ ((strpos($string,"<")>0)) echo "Your entry/username contained illegal characters!";
}

WhosAWhata

4:55 pm on Jun 9, 2004 (gmt 0)

10+ Year Member



i suppose that would work...interesting idea

WhosAWhata

4:57 pm on Jun 9, 2004 (gmt 0)

10+ Year Member



by the way in my earlier post i made a mistake
function clean($string) {
for($x=0,$x<=250;$x++){
if(($string[x] == "<") ¦¦ ($string[x] == ">")) {
echo "Your entry/username contained illegal characters!";
exit();
}
}
}

should be

function clean($string) {
for($x=0,$x<=250;$x++){
if(($string[$x] == "<") ¦¦ ($string[$x] == ">")) {
echo "Your entry/username contained illegal characters!";
exit();
}
}
}

WhosAWhata

5:03 pm on Jun 9, 2004 (gmt 0)

10+ Year Member



o and DevlshOne
there are tons of other ways
preg_match
if(str_replace
and many more
the great thing about PHP is the myriad of methods to get the job done