string to lowercase accentuation hyphenated

Solutions on MaxInterview for string to lowercase accentuation hyphenated by the best coders in the world

showing results for - "string to lowercase accentuation hyphenated"
Lou
10 Feb 2017
1<?php
2
3/**
4 * Converts string to SEO-friendly form (lowercase hyphenated alphanumeric words)
5 *
6 * @param $string
7 * @return string
8 */
9function seoUrl($string)
10{
11    // qv stackoverflow.com/questions/11330480, stackoverflow.com/questions/1017599
12
13    $src = 'àáâãäçèéêëìíîïñòóôõöøùúûüýÿßÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝ';
14    $rep = 'aaaaaceeeeiiiinoooooouuuuyysAAAAACEEEEIIIINOOOOOOUUUUY';
15
16    // strip off accents (assuming utf8 PHP - note strtr() requires single-byte)
17    $string = strtr(utf8_decode($string), utf8_decode($src), $rep);
18    // convert to lower case
19    $string = strtolower($string);
20    // strip all but alphanumeric, whitespace, dot, underscore, hyphen
21    $string = preg_replace("/[^a-z0-9\s._-]/", "", $string);
22    // merge multiple consecutive whitespaces, dots, underscores, hyphens
23    $string = preg_replace("/[\s._-]+/", " ", $string);
24    // convert whitespaces to hyphens
25    $string = preg_replace("/[\s]/", "-", $string);
26
27    return $string;
28}
similar questions