Skip to content

Instantly share code, notes, and snippets.

@lcosta
Created May 24, 2018 10:47
Show Gist options
  • Save lcosta/b1a0e03c34c6af9a65eac7c1153062c5 to your computer and use it in GitHub Desktop.
Save lcosta/b1a0e03c34c6af9a65eac7c1153062c5 to your computer and use it in GitHub Desktop.
<?php
/**
* prepares a string optimized for SEO
* @param string $sString
* @return string $sString SEO optimized string
*/
function seofy ($sString = '')
{
$sString = preg_replace ('/[^\pL\d_]+/u', '-', $sString);
$sString = trim ($sString, "-");
$sString = iconv ('utf-8', "us-ascii//TRANSLIT", $sString);
$sString = strtolower ($sString);
$sString = preg_replace ('/[^-a-z0-9_]+/', '', $sString);
return $sString;
}
echo seofy('Straßenfest in München'); // => strassenfest-in-muenchen
echo seofy('José Ignacio López de Arriortúa'); // => jose-ignacio-lopez-de-arriortua
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment