[英]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, ie $str[$i]='';
它可以工作,但是我不确定是否“允许”这样做,即
$str[$i]='';
. 。
Note: $str
is a string variable, $arAllowedCharset
is an array containing only alphanumerical characters and a dash. 注意:
$str
是一个字符串变量, $arAllowedCharset
是一个仅包含字母数字字符和破折号的数组。
I use that to format user-submitted URLs in a custom CMS. 我用它来格式化自定义CMS中用户提交的URL。
Thank you. 谢谢。
It would be easier to check and correct the url with a regular expression. 使用正则表达式检查并更正url会更容易。 For example
例如
$str = preg_replace('#[^a-z0-9-]#i', '', $str);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.