You are here: check string contents: can be used as var? « PHP Programming Language « IT news, forums, messages
check string contents: can be used as var?

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация