4

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 including 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"

2
  • 1
    Be careful though because "Peter's Pets Hop @ Washing" would also translate to the same string. Commented Sep 27, 2011 at 11:35
  • Yep, noticed that. Will be checking that the input must be unique Commented Sep 27, 2011 at 11:43

1 Answer 1

12

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

Sign up to request clarification or add additional context in comments.

1 Comment

Sweet, I tuned it a bit, making ' go away instead of - Thank you.

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.