开发者

PHP accessing String vars as Array and changing value

开发者 https://www.devze.com 2023-01-24 07:11 出处:网络
Is it ok to do this please? for($i=0;$i<strlen($str);$i++) { if(!in_array($str[$i],$arAllowedCharset)){$str[$i]=\'\';}

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]='';.

Note: $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);
0

精彩评论

暂无评论...
验证码 换一张
取 消