Skip to main content
added 11 characters in body
Source Link
LSerni
  • 57.7k
  • 10
  • 70
  • 110

Updateimproved clean-up

Update

improved clean-up

added 28 characters in body
Source Link
LSerni
  • 57.7k
  • 10
  • 70
  • 110
function hyphenize($string) { $dict = array( "I'm" => "I am", "thier" => "their", // Add your own replacements here ); return strtolower( preg_replace( array( '#[\\s-]+#', '#[^A-Za-z0-9\9. -]+#' ), array( '-', '' ), // the full cleanString() can be downloaded from http://www.unexpectedit.com/php/php-clean-string-of-utf8-chars-convert-to-similar-ascii-char cleanString( str_replace( // preg_replace can be used to support more complicated replacements array_keys($dict), array_values($dict), urldecode($string) ) ) ) ); } function cleanString($text) { $utf8 = array( '/[áàâãªä]/u' => 'a', '/[ÁÀÂÃÄ]/u' => 'A', '/[ÍÌÎÏ]/u' => 'I', '/[íìîï]/u' => 'i', '/[éèêë]/u' => 'e', '/[ÉÈÊË]/u' => 'E', '/[óòôõºö]/u' => 'o', '/[ÓÒÔÕÖ]/u' => 'O', '/[úùûü]/u' => 'u', '/[ÚÙÛÜ]/u' => 'U', '/ç/' => 'c', '/Ç/' => 'C', '/ñ/' => 'n', '/Ñ/' => 'N', '/–/' => '-', // UTF-8 hyphen to "normal" hyphen '/[’‘‹›‚]/u' => ' ', // Literally a single quote '/[“”«»„]/u' => ' ', // Double quote '/ /' => ' ', // nonbreaking space (equiv. to 0x160) ); return preg_replace(array_keys($utf8), array_values($utf8), $text); } 
// Remove all characters except A-Z, a-z, 0-9, dots, hyphens and spaces // Note that the hyphen must go last not to be confused with a range (A-Z) // and the dot, NOT being special (I know. My life was a lie), is escaped withNOT \escaped  $str = preg_replace('/[^A-Za-z0-9\9. -]/', '', $str); // Replace sequences of spaces with hyphen $str = preg_replace('/ */', '-', $str); // The above means "a space, followed by a space repeated zero or more times" // (should be equivalent to / +/) // You may also want to try this alternative: $str = preg_replace('/\\s+/', '-', $str); // where \s+ means "zero or more whitespaces" (a space is not necessarily the // same as a whitespace) just to be sure and include everything 
function hyphenize($string) { return ## strtolower( preg_replace( array('#[\\s-]+#', '#[^A-Za-z0-9\9. -]+#'), array('-', ''), ## cleanString( urldecode($string) ## ) ) ## ) ; } print implode("\n", array_map( function($s) { return $s . ' becomes ' . hyphenize($s); }, array( 'Never%20gonna%20give%20you%20up', "I'm not the man I was", "'Légeresse', dit sa majesté", ))); Never%20gonna%20give%20you%20up becomes never-gonna-give-you-up I'm not the man I was becomes im-not-the-man-I-was 'Légeresse', dit sa majesté becomes legeresse-dit-sa-majeste 
function hyphenize($string) { $dict = array( "I'm" => "I am", "thier" => "their", // Add your own replacements here ); return strtolower( preg_replace( array( '#[\\s-]+#', '#[^A-Za-z0-9\. -]+#' ), array( '-', '' ), // the full cleanString() can be downloaded from http://www.unexpectedit.com/php/php-clean-string-of-utf8-chars-convert-to-similar-ascii-char cleanString( str_replace( // preg_replace can be used to support more complicated replacements array_keys($dict), array_values($dict), urldecode($string) ) ) ) ); } function cleanString($text) { $utf8 = array( '/[áàâãªä]/u' => 'a', '/[ÁÀÂÃÄ]/u' => 'A', '/[ÍÌÎÏ]/u' => 'I', '/[íìîï]/u' => 'i', '/[éèêë]/u' => 'e', '/[ÉÈÊË]/u' => 'E', '/[óòôõºö]/u' => 'o', '/[ÓÒÔÕÖ]/u' => 'O', '/[úùûü]/u' => 'u', '/[ÚÙÛÜ]/u' => 'U', '/ç/' => 'c', '/Ç/' => 'C', '/ñ/' => 'n', '/Ñ/' => 'N', '/–/' => '-', // UTF-8 hyphen to "normal" hyphen '/[’‘‹›‚]/u' => ' ', // Literally a single quote '/[“”«»„]/u' => ' ', // Double quote '/ /' => ' ', // nonbreaking space (equiv. to 0x160) ); return preg_replace(array_keys($utf8), array_values($utf8), $text); } 
// Remove all characters except A-Z, a-z, 0-9, dots, hyphens and spaces // Note that the hyphen must go last not to be confused with a range (A-Z) // and the dot, being special, is escaped with \ $str = preg_replace('/[^A-Za-z0-9\. -]/', '', $str); // Replace sequences of spaces with hyphen $str = preg_replace('/ */', '-', $str); // The above means "a space, followed by a space repeated zero or more times" // (should be equivalent to / +/) // You may also want to try this alternative: $str = preg_replace('/\\s+/', '-', $str); // where \s+ means "zero or more whitespaces" (a space is not necessarily the // same as a whitespace) just to be sure and include everything 
function hyphenize($string) { return ## strtolower( preg_replace( array('#[\\s-]+#', '#[^A-Za-z0-9\. -]+#'), array('-', ''), ## cleanString( urldecode($string) ## ) ) ## ) ; } print implode("\n", array_map( function($s) { return $s . ' becomes ' . hyphenize($s); }, array( 'Never%20gonna%20give%20you%20up', "I'm not the man I was", "'Légeresse', dit sa majesté", ))); Never%20gonna%20give%20you%20up becomes never-gonna-give-you-up I'm not the man I was becomes im-not-the-man-I-was 'Légeresse', dit sa majesté becomes legeresse-dit-sa-majeste 
function hyphenize($string) { $dict = array( "I'm" => "I am", "thier" => "their", // Add your own replacements here ); return strtolower( preg_replace( array( '#[\\s-]+#', '#[^A-Za-z0-9. -]+#' ), array( '-', '' ), // the full cleanString() can be downloaded from http://www.unexpectedit.com/php/php-clean-string-of-utf8-chars-convert-to-similar-ascii-char cleanString( str_replace( // preg_replace can be used to support more complicated replacements array_keys($dict), array_values($dict), urldecode($string) ) ) ) ); } function cleanString($text) { $utf8 = array( '/[áàâãªä]/u' => 'a', '/[ÁÀÂÃÄ]/u' => 'A', '/[ÍÌÎÏ]/u' => 'I', '/[íìîï]/u' => 'i', '/[éèêë]/u' => 'e', '/[ÉÈÊË]/u' => 'E', '/[óòôõºö]/u' => 'o', '/[ÓÒÔÕÖ]/u' => 'O', '/[úùûü]/u' => 'u', '/[ÚÙÛÜ]/u' => 'U', '/ç/' => 'c', '/Ç/' => 'C', '/ñ/' => 'n', '/Ñ/' => 'N', '/–/' => '-', // UTF-8 hyphen to "normal" hyphen '/[’‘‹›‚]/u' => ' ', // Literally a single quote '/[“”«»„]/u' => ' ', // Double quote '/ /' => ' ', // nonbreaking space (equiv. to 0x160) ); return preg_replace(array_keys($utf8), array_values($utf8), $text); } 
// Remove all characters except A-Z, a-z, 0-9, dots, hyphens and spaces // Note that the hyphen must go last not to be confused with a range (A-Z) // and the dot, NOT being special (I know. My life was a lie), is NOT escaped  $str = preg_replace('/[^A-Za-z0-9. -]/', '', $str); // Replace sequences of spaces with hyphen $str = preg_replace('/ */', '-', $str); // The above means "a space, followed by a space repeated zero or more times" // (should be equivalent to / +/) // You may also want to try this alternative: $str = preg_replace('/\\s+/', '-', $str); // where \s+ means "zero or more whitespaces" (a space is not necessarily the // same as a whitespace) just to be sure and include everything 
function hyphenize($string) { return ## strtolower( preg_replace( array('#[\\s-]+#', '#[^A-Za-z0-9. -]+#'), array('-', ''), ## cleanString( urldecode($string) ## ) ) ## ) ; } print implode("\n", array_map( function($s) { return $s . ' becomes ' . hyphenize($s); }, array( 'Never%20gonna%20give%20you%20up', "I'm not the man I was", "'Légeresse', dit sa majesté", ))); Never%20gonna%20give%20you%20up becomes never-gonna-give-you-up I'm not the man I was becomes im-not-the-man-I-was 'Légeresse', dit sa majesté becomes legeresse-dit-sa-majeste 
added 181 characters in body
Source Link
LSerni
  • 57.7k
  • 10
  • 70
  • 110

To handle UTF-8 I used a cleanString implementation found online here(link broken since, but a stripped down copy with all the not-too-esoteric UTF8 characters is at the beginning of the answer; it's also easy to add more characters to it if you need) that converts UTF8 characters to normal characters, thus preserving the word "look" as much as possible. It could be simplified and wrapped inside the function here for performance.

To handle UTF-8 I used a cleanString implementation found here. It could be simplified and wrapped inside the function here for performance.

To handle UTF-8 I used a cleanString implementation found online (link broken since, but a stripped down copy with all the not-too-esoteric UTF8 characters is at the beginning of the answer; it's also easy to add more characters to it if you need) that converts UTF8 characters to normal characters, thus preserving the word "look" as much as possible. It could be simplified and wrapped inside the function here for performance.

added 114 characters in body
Source Link
LSerni
  • 57.7k
  • 10
  • 70
  • 110
Loading
added 923 characters in body
Source Link
LSerni
  • 57.7k
  • 10
  • 70
  • 110
Loading
added 141 characters in body
Source Link
LSerni
  • 57.7k
  • 10
  • 70
  • 110
Loading
added 1933 characters in body
Source Link
LSerni
  • 57.7k
  • 10
  • 70
  • 110
Loading
added 1 characters in body
Source Link
LSerni
  • 57.7k
  • 10
  • 70
  • 110
Loading
added 1276 characters in body
Source Link
LSerni
  • 57.7k
  • 10
  • 70
  • 110
Loading
Source Link
LSerni
  • 57.7k
  • 10
  • 70
  • 110
Loading