Skip to content

Instantly share code, notes, and snippets.

@karlgroves
Created December 6, 2013 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlgroves/7824109 to your computer and use it in GitHub Desktop.
Save karlgroves/7824109 to your computer and use it in GitHub Desktop.
/**
* cleans up strings so they can be used in URLS
* @author "Borek" - attributed to a post located at:
* http://drupal.org/node/63924
* @param string $string the string we're cleaning
* @return string the input string, ready to go
*/
function pathauto_cleanstring($string)
{
$url = $string;
$url = preg_replace('~[^\\pL0-9_]+~u', '-', $url); // substitutes anything but letters, numbers and '_' with separator
$url = trim($url, "-");
$url = iconv("utf-8", "us-ascii//TRANSLIT", $url); // TRANSLIT does the whole job
$url = strtolower($url);
$url = preg_replace('~[^-a-z0-9_]+~', '', $url); // keep only letters, numbers, '_' and separator
return $url;
}
@steffenr
Copy link

steffenr commented Oct 23, 2023

For Drupal 8 and above, you can use the following code:

$clean_string = \Drupal::service('pathauto.alias_cleaner')->cleanString('Your very dirty string, with many URL un-friendly parts!');`

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