Skip to content

Instantly share code, notes, and snippets.

@meSingh
Created July 1, 2013 13:35
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save meSingh/5900814 to your computer and use it in GitHub Desktop.
Save meSingh/5900814 to your computer and use it in GitHub Desktop.
Create WordPress like slugs from any string in php.
<?php
/**
*
* Genrate Slugs from Title or any given string
*
* @param string $str
* @return Slug
*
**/
public function make_slug($str)
{
if($str !== mb_convert_encoding( mb_convert_encoding($str, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32') )
$str = mb_convert_encoding($str, 'UTF-8', mb_detect_encoding($str));
$str = htmlentities($str, ENT_NOQUOTES, 'UTF-8');
$str = preg_replace('`&([a-z]{1,2})(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i', '\\1', $str);
$str = html_entity_decode($str, ENT_NOQUOTES, 'UTF-8');
$str = preg_replace(array('`[^a-z0-9]`i','`[-]+`'), '-', $str);
$str = strtolower( trim($str, '-') );
$str = substr($str, 0, 100);
return $str;
}
@rkbwp
Copy link

rkbwp commented Mar 31, 2020

It works only for English.

@meSingh
Copy link
Author

meSingh commented Mar 31, 2020

It works only for English.

Honestly, I'm surprised that it still works. It's been seven years since I created it.
You can try this one: https://gist.github.com/tlongren/5527129

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment