开发者

PHP Remove url-"not allowed" characters from string

开发者 https://www.devze.com 2023-04-09 05:33 出处:网络
I have read through all the questions regarding this, and would like to know if $str = preg_replace(\'/[^\\00-\\255]+/u\', \'\', $str); is sufficient for my scenario.

I have read through all the questions regarding this, and would like to know if $str = preg_replace('/[^\00-\255]+/u', '', $str); is sufficient for my scenario.

My Scenario

When a user creates an account on my site, he enters his company's name. This can be anything i开发者_StackOverflow中文版ncluding text with ' or " or even some other strange characters. When he creates an account, I need to create a folder on my server for him to access his account easier without using uniqids etc.

So for example you create an account for "Peter's Pet Shop & Washing" - I would need to remove all spaces and characters that would not be allowed as a url-address. So at the end I need to have "peterspetshopwashing"

This is so that you can access your account at "www.mydomain.com/peterspetshopwashing"


I currently use this function I'm happy with

function url($url) {
   $url = preg_replace('~[^\\pL0-9_]+~u', '-', $url);
   $url = trim($url, "-");
   $url = iconv("utf-8", "us-ascii//TRANSLIT", $url);
   $url = strtolower($url);
   $url = preg_replace('~[^-a-z0-9_]+~', '', $url);
   return $url;
}

it replaces spaces and other odd characters with - so result will be peter-s-pet-shop-washing

0

精彩评论

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