Posted by Oliver Saunders on 10/18/05 20:21
is there anyway to check if a string could be used a variable name. So
return false if the first character isn't a letter or if it contains
spaces etc.
I started writing a function that would do a load of tests because I
couldn't see any easier way of doing it:
<?
function is_valid_handle($str) {
// determines if the string provided can be used as a handle
// such as a php variable or html/css class
if (strlen($str) > 255)) return false;
if (strpos($str,' ')) return false;
// incomplete, needs more tests
return true;
}
?>
but as soon as i realised i would have to use range() to check all the
characters were valid and all that i decided there must be an easier
way. Do you guys know of one or am I just going to have to write it myself?
It was about then I had a great idea maybe if you use the variable
variable syntax you can see if it will return false but unfortunately it
does this:
<?
$foo= '5very @#bad variable name';
$$foo = 'Hi!'; // doesn't cause error
echo $$foo; // outputs Hi!
echo $5very @#bad variable name; // causes an error, obviously
?>
Seems PHP will quite happily let you store data in a variable of
whatever you like. I guess that's a nice feature. So...any ideas?
Navigation:
[Reply to this message]
|