Search code examples
urlutf-8slug

How do you make a better seo friendly URL when there is UTF-8 characters and many space in between?


When someone by mistake enters many spaces between characters what I do is to replace all spaces with - but what if there are many spaces in between? for e.g:

User entered post title:

فارسی   * Allposts---

When I convert the above example to user-friendly url (slug) I get this:

----فارسی---*-Allposts---

How to put one - for spaces and remove special characters and preserve utf-8 characters as well? The output I'm seeking for is as below:

فارسی-Allposts  

Is there a way to handle it with regex? if positive, how?


EDIT:

Now I can manage multiple spaces as below:

 $string = preg_replace('/\s+/', '-', $string);

but for special chars problem still remains.


Solution

    1. Remove special characters: replace [\-\?\*] or whatever your blacklist characters are with empty string.
    2. Convert strings of whitespace to a single - character: replace \s+ with -

    Looks like you already figured out step 2. Make sure you do it second so you don't accidentally remove your own hyphens that you just inserted.