PHP accessing String vars as Array and changing value
Is it ok to do this please?
for($i=0;$i<strlen($str);$i++)
{
if(!in_array($str[$i],$arAllowedCharset)){$str[$i]='';}
}
return $str;
It works, but I am unsure if I am "allowed" to do that, i.e. $str[$i]='';
.
$str
is a string variable, $arAllowedCharset
is an array containing only alphanu开发者_Python百科merical characters and a dash.
I use that to format user-submitted URLs in a custom CMS.
Thank you.
It would be easier to check and correct the url with a regular expression. For example
$str = preg_replace('#[^a-z0-9-]#i', '', $str);
精彩评论