Is there a PHP function to create a pretty and valid url from natural text?

I want to automatically generate readable URLs from any natural text, for example:

Last article: About German Letters - Appeal by äöü and ß!

ideally will be changed to this

last-article-o-german-letters-appeal-AOU-and-ss.html

It should work for all Latin languages, and I want to avoid any way out.

I suppose this can be achieved with regular expressions, but perhaps the standard function already available in PHP / PEAR / PECL already exists.

+4
source share
7 answers

What you are looking for removes your text.

You can find code snippets on the Internet, such as this one that will do the trick:

/** * Modifies a string to remove al non ASCII characters and spaces. */ static public function slugify($text) { // replace non letter or digits by - $text = preg_replace('~[^\\pL\d]+~u', '-', $text); // trim $text = trim($text, '-'); // transliterate if (function_exists('iconv')) { $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); } // lowercase $text = strtolower($text); // remove unwanted characters $text = preg_replace('~[^-\w]+~', '', $text); if (empty($text)) { return 'n-a'; } return $text; } 

Of.

+11
source

I don't think there is a function for this, I recently created this:

 function fix_url($word) { /** * whilst the descriptor in the url will be for SEO * purposes only, we need to ensure it doesn't break * the URI rules http://www.faqs.org/rfcs/rfc2396.html */ // convert to lower case $word=strtolower($word); // define illegal / replacement characters $illegal = array("ä","ö","ü","ß"); $replace = array("a","o","u","ss"); $word = str_replace($illegal, $replace, $word); // remove & for and $word=str_replace("&","and",$word); // remove a space for - $word=str_replace(" ","-",$word); // and replace all non alphanumeric characters or a dash $word=ereg_replace("[^A-Za-z0-9-]", "", $word); return $word; } 

I included an example of replacing an illegal character with a safe one.

I tested this code and it returns latest-article-about-german-letters---handling-aou-and-ss , so obviously there are some more settings (see ---), but I'm sure it will be easy to adapt.

+3
source

For some time now I have been successfully using utf8_to_ascii from PHP UTF8 . Works for any UTF-8 text (including non-Latin).

+1
source

create an array with your special characters, pass through them with str_replace and replace your values ​​with the desired value.

0
source

You will definitely need to replace special characters. Then you can use preg_replace and do something like

 $url = preg_replace("#[^a-zA-Z0-9_-]#", "_", $string); 
0
source

I use the following to create a file name from a string:

 function format_filename($str) { $str = preg_replace('/[^A-Za-z0-9- ]/', $seperator, $str); $str = str_replace($seperator, '', $str); $str = str_replace(' ', $seperator, $str); return strtolower($str); } 

It was quick and dirty, and I'm sure that some of the other good people here can improve this piece of code.

0
source

I found this slugify function on GitHub that works great for me. It also replaces the German umlauts (for example: ä → ae).

https://github.com/mintao/yii-behavior-sluggable/blob/master/Doctrine_Inflector.php

 $url = Doctrine_Inflector::urlize($text); 
0
source

All Articles