note 122374 deleted from function.preg-replace by crell
| From: | crell@php.net | Date: | Fri, 01 Jul 2022 19:46:42 +0000 |
| Subject: | note 122374 deleted from function.preg-replace by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218853@lists.php.net to get a copy of this message | ||
Note Submitter: creating SEO friendly Strings
----
<?php
/**
* prepares a string optimized for SEO
* @see https://blog.ueffing.net/post/2016/03/14/string-seo-optimieren-creating-seo-friendly-url/
* @param String $string
* @return String $string 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;
}
// Example
seofy('StraÃenfest in München'); // => strassenfest-in-muenchen
seofy('José Ignacio López de Arriortúa'); // =>
jose-ignacio-lopez-de-arriortua
?>
The function seofy () creates a SEO friendly version from a string. Umlauts and other letters not
contained in the ASCII character set are either reduced to the basic form equivalent (e. g.: é
becomes e and ú wid u) or completely converted (e. g. à becomes ss and ü becomes ue).
On the one hand this succeeds because the php function preg_replace performs the replacement by
means of unicode - Unicode Regular Expressions - and on the other hand because an approximate
translation is attempted by means of the php function iconv with the TRANSLIT option.
Quote php. net about iconv and TRANSLIT:
"If you append the character string //TRANSLIT to out_charset, transliteration is activated.
This means that a character that cannot be displayed in the target character set can be approximated
with one or more similar-looking characters.[â¦]"
Source:
https://blog.ueffing.net/post/2016/03/14/string-seo-optimieren-creating-seo-friendly-url/