PDA

View Full Version : I need help with my PHP Script


noRush
Fri 11th Jul '03, 10:54am
for ($i=0; $i < 256; $i++)
{
if ($str == chr($i))
{
$answer = chr($i);
}
}
if(isset($answer))print $answer;
else die();

For security reasons I can't just go print out $str, but I need to check it for my own use, so I wrote that litte script. But however, that only do when $str has only one character, but not two... note that $str would never be null.

Son-Goku
Fri 11th Jul '03, 12:15pm
for ($i=0; $i < 256; $i++)
{
if ($str == chr($i))
{
$answer = chr($i);
}
}
if(isset($answer))print $answer;
else die();

For security reasons I can't just go print out $str, but I need to check it for my own use, so I wrote that litte script. But however, that only do when $str has only one character, but not two... note that $str would never be null.

imho you'd be better off using
if(preg_match("/^[ALL_ALLOWED_ASCII_CHARACTERS]$/", $str)) {
$answer = $str;
} else {
exit;
}
(look at the php docu for the exact use, i didn't use it for quite a while...).

noRush
Fri 11th Jul '03, 12:20pm
preg_match("/^[ALL_ALLOWED_ASCII_CHARACTERS]$/", $str)

How would this work? It would only works for one character but what if there is 2 or more?

Son-Goku
Fri 11th Jul '03, 1:41pm
preg_match("/^[ALL_ALLOWED_ASCII_CHARACTERS]$/", $str)

How would this work? It would only works for one character but what if there is 2 or more?
no, preg_match will search through the whole string. for example preg_match would return true for "abc123" if you'd have /^[a-c1-3]+$/ as your regular expression. it would also return true if the string was "c1ba32" or anything containing those "valid characters".

[a-zA-Z0-9]+ would allow every string containing a-z A-Z 0-9 with a variable length. (+ stands for at least one of those allowed characters, ? stands for 0 or 1 and * for 0 or more iirc). you can also define the min and max length of the possible string yourself with {min, max} instead of +. for a better explaination (a lot better :p) just go to http://www.php.net/preg_match

Stadler
Fri 11th Jul '03, 3:23pm
[ALL_ALLOWED_ASCII_CHARACTERS] would be [\x00-\xFF]

Or you use preg_match("/^.+$/s", $str);

. = a placeholder for all Chars but \n

/.../s = PCRE_DOTALL: the dot becomes a placeholder for all chars including \n (See www.php.net/manual/de/pcre.pattern.modifiers.php)

+ = quantifier for at least one char (={1,} See www.php.net/manual/de/pcre.pattern.syntax.php#regexp.reference.repetitio n)

To learn more about PCRE, check www.php.net/pcre

HTH

noRush
Sat 12th Jul '03, 9:02am
But that returns a true : false only, if the $str is going to be a string then what is the point of doing that? I need to know what $str is...

Ok I'll try explain this situation that I have: $str is a very sensitive string that I don't want to do anything to it, I can't use any functions and not even the print function when the print function is really considered as a language construct. But I need to know what $str is, so I have to somehow compare them with all characters from the ascii table. The ver first script that I posted is fine if $str contains only ONE character, but can't do anymore when there is two or more.

Thank you!