Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string into slug with single-hyphen delimiters only

I would like to sanitize a string in to a URL so this is what I basically need:

  1. Everything must be removed except alphanumeric characters and spaces and dashed.
  2. Spaces should be converter into dashes.

Eg.

This, is the URL!

must return

this-is-the-url
like image 712
Atif Avatar asked Jun 11 '10 11:06

Atif


5 Answers

function slug($z){
    $z = strtolower($z);
    $z = preg_replace('/[^a-z0-9 -]+/', '', $z);
    $z = str_replace(' ', '-', $z);
    return trim($z, '-');
}
like image 60
SilentGhost Avatar answered Oct 01 '22 16:10

SilentGhost


First strip unwanted characters

$new_string = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);

Then changes spaces for unserscores

$url = preg_replace('/\s/', '-', $new_string);

Finally encode it ready for use

$new_url = urlencode($url);
like image 33
Rooneyl Avatar answered Oct 01 '22 17:10

Rooneyl


This will do it in a Unix shell (I just tried it on my MacOS):

$ tr -cs A-Za-z '-' < infile.txt > outfile.txt

I got the idea from a blog post on More Shell, Less Egg

like image 23
user1484291 Avatar answered Oct 01 '22 18:10

user1484291


Try This

 function clean($string) {
       $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
       $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

       return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
    }

Usage:

echo clean('a|"bc!@£de^&$f g');

Will output: abcdef-g

source : https://stackoverflow.com/a/14114419/2439715

like image 32
Abhishek Goel Avatar answered Oct 01 '22 16:10

Abhishek Goel


The OP is not explicitly describing all of the attributes of a slug, but this is what I am gathering from the intent.

My interpretation of a perfect, valid, condensed slug aligns with this post: https://wordpress.stackexchange.com/questions/149191/slug-formatting-acceptable-characters#:~:text=However%2C%20we%20can%20summarise%20the,or%20end%20with%20a%20hyphen.

I find none of the earlier posted answers to achieve this consistently (and I'm not even stretching the scope of the question to include multi-byte characters).

  1. convert all characters to lowercase
  2. replace all sequences of one or more non-alphanumeric characters to a single hyphen.
  3. trim the leading and trailing hyphens from the string.

I recommend the following one-liner which doesn't bother declaring single-use variables:

return trim(preg_replace('/[^a-z0-9]+/', '-', strtolower($string)), '-');

I have also prepared a demonstration which highlights what I consider to be inaccuracies in the other answers. (Demo)

'This, is - - the URL!' input
'this-is-the-url'       expected

'this-is-----the-url'   SilentGhost
'this-is-the-url'       mario
'This-is---the-URL'     Rooneyl
'This-is-the-URL'       AbhishekGoel
'This, is - - the URL!' HelloHack
'This, is - - the URL!' DenisMatafonov
'This,-is-----the-URL!' AdeelRazaAzeemi
'this-is-the-url'       mickmackusa

---
'Mork & Mindy'      input
'mork-mindy'        expected

'mork--mindy'       SilentGhost
'mork-mindy'        mario
'Mork--Mindy'       Rooneyl
'Mork-Mindy'        AbhishekGoel
'Mork &amp; Mindy'  HelloHack
'Mork & Mindy'      DenisMatafonov
'Mork-&-Mindy'      AdeelRazaAzeemi
'mork-mindy'        mickmackusa

---
'What the_underscore ?!?'   input
'what-the-underscore'       expected

'what-theunderscore'        SilentGhost
'what-the_underscore'       mario
'What-theunderscore-'       Rooneyl
'What-theunderscore-'       AbhishekGoel
'What the_underscore ?!?'   HelloHack
'What the_underscore ?!?'   DenisMatafonov
'What-the_underscore-?!?'   AdeelRazaAzeemi
'what-the-underscore'       mickmackusa
like image 41
mickmackusa Avatar answered Oct 01 '22 18:10

mickmackusa