Re: Stripping Characters

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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:
> 
> $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:
> 
> >
> >
> > On Jun 22, 2010, at 1:41 PM, Ashley Sheridan wrote:
> >>
> >> It 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, spaces might  
> >> become 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.
> >
> > 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.
> >
> > --Rick
> >
> 
> 


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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux