Re: Stripping Characters
| From: | Rick Dwyer | Date: | Wed, 23 Jun 2010 00:21:17 +0000 |
| Subject: | Re: Stripping Characters | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-306324@lists.php.net to get a copy of this message | ||
Very good.
Thank you.
--Rick
On Jun 22, 2010, at 8:14 PM, Ashley Sheridan wrote:
On Tue, 2010-06-22 at 20:03 -0400, Rick Dwyer wrote:Hello again list. My code for stripping characters is below. I'm hoping to get feedback as to how rock solid it will provide the desired output under any circumstance: My output must look like this (no quotes): "This-is-my-string-with-lots-of-junk-characters-in-it" The code with string looks like this:On Jun 22, 2010, at 1:41 PM, Ashley Sheridan wrote:$old_string = 'ééééThis is my & $string -- with ƒlots˙˙˙of junk characters in it¡™£¢∞§¶•ªºœ∑´®† ¥¨ˆøπ“‘ååååååß∂ƒ©˙∆˚¬…æ`````````__________'; $find = '/[^a-z0-9]/i'; $replace = ' '; $new_string = preg_replace($find, $replace, $old_string); $new_string = preg_replace("/ {2,}/", "-", $new_string); $new_string = preg_replace("/ {1,}/", "-", $new_string); $new_string = rtrim($new_string, "-"); $new_string = ltrim($new_string, "-"); echo $new_string; Will the logic above capture and remove every non alpha numeric character and place a SINGLE hyphen between the non contiguous alpha numeric characters? Thanks for the help on this. --Rick On Jun 22, 2010, at 4:52 PM, Rick Dwyer wrote:No, actually I'm fairly confident characters outside the 128 range are what are causing me problems now. So I will try Richard's method. Thanks to all. --RickmightIt is clean, but as Richard mentioned, it won't handle strings outside of the traditional 128 ASCII range, so accented characters and the like will be converted to an underscore. Also, spacesbecome an issue. However, if you are happy that your input won't go beyond the a- z0-9 range, then it should do what you need.You can remove the second line of code, as the third one is replacing what line 2 does anyway. Also, instead of a rtrim and ltrim, you can merge the two with a single call to trim, which will work on both ends of the string at once. Thanks, Ash http://www.ashleysheridan.co.uk