11

strtofname

strtofname
SYNOPSIS

strtofname($s, $strict=false)

DESCRIPTION

strtofname returns the conversion of the string $s into a file name by removing all accents and replacing special characters with a dash.

CODE
  1. require_once 'strflat.php';
  2.  
  3. function strtofname($s, $strict=false) {
  4.     /* remove accents */
  5.     $s = strflat($s);
  6.  
  7.     /* lower case */
  8.     $s = strtolower($s);
  9.  
  10.     /* keep letters, digits, underscores and dashes replacing others by a dash */
  11.     $s = preg_replace('#[^a-z0-9_-]#', '-', $s);
  12.  
  13.     /* replace consecutive dashes by one */
  14.     $s = preg_replace('/[-]+/', '-', $s);
  15.  
  16.     /* remove a dash at the beginning or at the end */
  17.     $s = preg_replace('/^-|-$/', '', $s);
  18.  
  19.     if (!$strict) {
  20.         return $s;
  21.     }
  22.  
  23.     /* remove words which are too short */
  24.     $r = array();
  25.     foreach (explode('-', $s) as $w)    {
  26.         if (strlen($w) > 2) {
  27.             $r[] = $w;
  28.         }
  29.     }
  30.  
  31.     return implode('-', $r);
  32. }
SEE ALSO

strflat

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].