8

strtofname

strtofname
SYNOPSIS

strtofname($s, $strict=false)

DESCRIPTION

strtofname returns the convertions of $s into a filename by removing all accents and replacing special characters with a dash.

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