1

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 alphanumerical characters and a dash.

I use that to format user-submitted URLs in a custom CMS.

Thank you.

1 Answer 1

5

It would be easier to check and correct the url with a regular expression. For example

$str = preg_replace('#[^a-z0-9-]#i', '', $str);
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, thanks. I wasn't sure if it was worth firing up the RegExp engine just for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.