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

To add a comment, click here.