Skip to content

URLify

A fast PHP slug generator and transliteration library, started as a PHP port of URLify.js from the Django project + fallback via "Portable ASCII".

  • https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/urlify.js
  • https://github.com/voku/portable-ascii

Handles symbols from latin languages, Arabic, Azerbaijani, Bulgarian, Burmese, Croatian, Czech, Danish, Esperanto, Estonian, Finnish, French, Switzerland (French), Austrian (French), Georgian, German, Switzerland (German), Austrian (German), Greek, Hindi, Kazakh, Latvian, Lithuanian, Norwegian, Persian, Polish, Romanian, Russian, Swedish, Serbian, Slovak, Turkish, Ukrainian and Vietnamese ... and many other via "ASCII::to_transliterate()".

  • Full name: \URLify

Properties

maps

The language-mapping array.

public static array[] $maps

ISO 639-1 codes: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

  • This property is static.

remove_list

List of words to remove from URLs.

public static array[] $remove_list
  • This property is static.

arrayToSeparator

An array of strings that will convert into the separator-char - used by "URLify::filter()".

private static string[] $arrayToSeparator
  • This property is static.

Methods

add_array_to_separator

Add new strings the will be replaced with the separator.

public static add_array_to_separator(array $array, bool $merge = true): void
  • This method is static.

Parameters:

Parameter Type Description
$array array <p>An array of things that should replaced by the separator.</p>
$merge bool <p>Keep the previous (default) array-to-separator array.</p>

add_chars

Add new characters to the list. $map should be a hash.

public static add_chars(array $map, string|null $language = null): void
  • This method is static.

Parameters:

Parameter Type Description
$map array
$language string|null

reset_chars

public static reset_chars(): void
  • This method is static.

downcode

Transliterates characters to their ASCII equivalents.

public static downcode(string $string, string $language = &#039;en&#039;, string $unknown = &#039;&#039;): string

$language specifies a priority for a specific language. The latter is useful if languages have different rules for the same character.

  • This method is static.

Parameters:

Parameter Type Description
$string string <p>The input string.</p>
$language string <p>Your primary language.</p>
$unknown string <p>Character use if character unknown. (default is ?).</p>

slug

Convert a String to URL slug. Wraps filter() with a simpler set of defaults for typical usage in generating blog post slugs.

public static slug(string $string, int $maxLength = 200, string $separator = &#039;-&#039;, string $language = &#039;en&#039;): string
  • This method is static.

Parameters:

Parameter Type Description
$string string <p>The text you want to convert.</p>
$maxLength int <p>Max. length of the output string, set to "0" (zero) to
disable it</p>
$separator string <p>Define a new separator for the words.</p>
$language string <p>The language you want to convert to.</p>

filter

Convert a String to URL.

public static filter(string $string, int $maxLength = 200, string $language = &#039;en&#039;, bool $fileName = false, bool $removeWords = false, bool $strToLower = true, bool|string $separator = &#039;-&#039;): string

e.g.: "Petty
theft" to "Petty-theft"

  • This method is static.

Parameters:

Parameter Type Description
$string string <p>The text you want to convert.</p>
$maxLength int <p>Max. length of the output string, set to "0" (zero) to
disable it</p>
$language string <p>The language you want to convert to.</p>
$fileName bool <p>
Keep the "." from the extension e.g.: "imaƤe.jpg" =>
"image.jpg"
</p>
$removeWords bool <p>
Remove some "words" from the string.<br />
Info: Set extra words via <strong>remove_words()</strong>.
</p>
$strToLower bool <p>Use <strong>strtolower()</strong> at the end.</p>
$separator bool|string <p>Define a new separator for the words.</p>

remove_words

Append words to the remove list. Accepts either single words or an array of words.

public static remove_words(string|string[] $words, string $language = &#039;en&#039;, bool $merge = true): void
  • This method is static.

Parameters:

Parameter Type Description
$words string|string[]
$language string
$merge bool <p>Keep the previous (default) remove-words array.</p>

reset_array_to_separator

Reset the internal "self::$arrayToSeparator" to the default values.

public static reset_array_to_separator(): void
  • This method is static.

reset_remove_list

reset the word-remove-array

public static reset_remove_list(string $language = &#039;en&#039;): void
  • This method is static.

Parameters:

Parameter Type Description
$language string

transliterate

Alias of URLify::downcode().

public static transliterate(string $string, string $language = &#039;en&#039;): string
  • This method is static.

Parameters:

Parameter Type Description
$string string
$language string

expandString

Expands the given string replacing some special parts for words.

protected static expandString(string $string, string $language = &#039;en&#039;): string

e.g. "lorem@ipsum.com" is replaced by "lorem at ipsum dot com".

Most of these transformations have been inspired by the pelle/slugger project, distributed under the Eclipse Public License. Copyright 2012 Pelle Braendgaard

  • This method is static.

Parameters:

Parameter Type Description
$string string The string to expand
$language string

Return Value:

The result of expanding the string


get_language_for_reset_remove_list

private static get_language_for_reset_remove_list(string $language): string
  • This method is static.

Parameters:

Parameter Type Description
$language string

expandCurrencies

Expands the numeric currencies in euros, dollars, pounds and yens that the given string may include.

private static expandCurrencies(string $string, string $language = &#039;en&#039;): string
  • This method is static.

Parameters:

Parameter Type Description
$string string
$language string

expandSymbols

Expands the special symbols that the given string may include, such as '@', '.', '#' and '%'.

private static expandSymbols(string $string, string $language = &#039;en&#039;): string
  • This method is static.

Parameters:

Parameter Type Description
$string string
$language string

get_remove_list

return the "self::$remove_list[$language]" array

private static get_remove_list(string $language = &#039;en&#039;): array
  • This method is static.

Parameters:

Parameter Type Description
$language string


Automatically generated on 2025-03-18