note 79951 deleted from function.preg-replace by felipe

From: Date: Sun, 23 Mar 2008 22:59:28 +0000
Subject: note 79951 deleted from function.preg-replace by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-137396@lists.php.net to get a copy of this message
Note Submitter: chrisadams at 407x dot com ---- Here is a usage that I found convenient for cleaning up file names that have been entered through a web form. The file names had to be preserved at this point (they will be renamed into machine-friendly names after they are processed). I used Hex code ranges to exclude undesirable characters. <?php function removeSpacesFromFileNames($imageName){ $patternCounter=0; $patterns[$patternCounter] = '/[\x21-\x2d]/u'; // remove range of shifted characters on keyboard - !"#$%&'()*+,- $patternCounter++; $patterns[$patternCounter] = '/[\x5b-\x60]/u'; // remove range including brackets - []\^_` $patternCounter++; $patterns[$patternCounter] = '/[\x7b-\xff]/u'; // remove all characters above the letter z. This will eliminate some non-English language letters $patternCounter++; $replacement ="_"; return preg_replace($patterns, $replacement, $imageName); } // so now: $goodFileName=removeSpacesFromFileNames('this is t$h B4aaaB4x! q?u"Best.png'); echo "output: ".$goodFileName; ?> output: this_is_t_h__aaa_x__q_u_Best.png

« previous php.notes (#137396) next »