Re: [PHP] Stripping Characters
On 23 June 2010 01:03, Rick Dwyer wrote: > $find = '/[^a-z0-9]/i'; Replace that with ... $find = '/[^a-z0-9]++/i'; And now you only need ... $new_string = trim(preg_replace($find, $replace, $old_string)); -- - Richard Quadling "Standing on the shoulders of some very clever giants!" EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Stripping Characters
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: $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
Re: [PHP] Stripping Characters
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
Re: [PHP] Stripping Characters
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Stripping Characters
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
Re: [PHP] Stripping Characters
Shreyas Agasthya wrote: > Then, when does one use ereg_replace as against preg_replace? I read from > one the forums that preg_* is faster and ereg_* is if not faster but > simpler. BUT, all the ereg_* has been depricated. DO NOT USE THEM if you want your code to work in the future. :) > > Is that it? > > Regards, > Shreyas > > > > On Tue, Jun 22, 2010 at 9:58 PM, Richard Quadling wrote: > >> "A "word" character is any letter or digit or the underscore >> character, that is, any character which can be part of a Perl "word". >> The definition of letters and digits is controlled by PCRE's character >> tables, and may vary if locale-specific matching is taking place. For >> example, in the "fr" (French) locale, some character codes greater >> than 128 are used for accented letters, and these are matched by \w." >> >> The above becomes ... >> >> _A _word_ character is any letter or digit or the underscore >> character_ that is_ any character which can be part of a Perl _word__ >> The definition of letters and digits is controlled by PCRE_s character >> tables_ and may vary if locale_specific matching is taking place_ For >> example_ in the _fr_ _French_ locale_ some character codes greater >> than 128 are used for accented letters_ and these are matched by _w__ >> >> -- >> - >> Richard Quadling >> "Standing on the shoulders of some very clever giants!" >> EE : http://www.experts-exchange.com/M_248814.html >> EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp >> Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 >> ZOPA : http://uk.zopa.com/member/RQuadling >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > > -- Jim Lucas A: Maybe because some people are too annoyed by top-posting. Q: Why do I not get an answer to my question(s)? A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Stripping Characters
On Tue, 2010-06-22 at 13:35 -0400, Rick Dwyer wrote: > Thanks to everyone who responded. > > Regarding the myriad of choices, isn't Ashley's, listed below, the one > most like to guarantee the cleanest output of just letters and numbers? > > > --Rick > > > On Jun 22, 2010, at 11:44 AM, Ashley Sheridan wrote: > > > On Tue, 2010-06-22 at 11:40 -0400, Rick Dwyer wrote: > > Use preg_replace(), which allows you to use a regex to specify what > > you want to match: > > > > $find = '/[^a-z0-9]/i'; > > $replace = '_'; > > $new_string = preg_replace($find, $replace, $old_string); > > > > Thanks, > > Ash > > http://www.ashleysheridan.co.uk > > > > > 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. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] Stripping Characters
Thanks to everyone who responded. Regarding the myriad of choices, isn't Ashley's, listed below, the one most like to guarantee the cleanest output of just letters and numbers? --Rick On Jun 22, 2010, at 11:44 AM, Ashley Sheridan wrote: On Tue, 2010-06-22 at 11:40 -0400, Rick Dwyer wrote: Use preg_replace(), which allows you to use a regex to specify what you want to match: $find = '/[^a-z0-9]/i'; $replace = '_'; $new_string = preg_replace($find, $replace, $old_string); Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] Stripping Characters
Then, when does one use ereg_replace as against preg_replace? I read from one the forums that preg_* is faster and ereg_* is if not faster but simpler. Is that it? Regards, Shreyas On Tue, Jun 22, 2010 at 9:58 PM, Richard Quadling wrote: > "A "word" character is any letter or digit or the underscore > character, that is, any character which can be part of a Perl "word". > The definition of letters and digits is controlled by PCRE's character > tables, and may vary if locale-specific matching is taking place. For > example, in the "fr" (French) locale, some character codes greater > than 128 are used for accented letters, and these are matched by \w." > > The above becomes ... > > _A _word_ character is any letter or digit or the underscore > character_ that is_ any character which can be part of a Perl _word__ > The definition of letters and digits is controlled by PCRE_s character > tables_ and may vary if locale_specific matching is taking place_ For > example_ in the _fr_ _French_ locale_ some character codes greater > than 128 are used for accented letters_ and these are matched by _w__ > > -- > - > Richard Quadling > "Standing on the shoulders of some very clever giants!" > EE : http://www.experts-exchange.com/M_248814.html > EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp > Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 > ZOPA : http://uk.zopa.com/member/RQuadling > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Regards, Shreyas Agasthya
Re: [PHP] Stripping Characters
"A "word" character is any letter or digit or the underscore character, that is, any character which can be part of a Perl "word". The definition of letters and digits is controlled by PCRE's character tables, and may vary if locale-specific matching is taking place. For example, in the "fr" (French) locale, some character codes greater than 128 are used for accented letters, and these are matched by \w." The above becomes ... _A _word_ character is any letter or digit or the underscore character_ that is_ any character which can be part of a Perl _word__ The definition of letters and digits is controlled by PCRE_s character tables_ and may vary if locale_specific matching is taking place_ For example_ in the _fr_ _French_ locale_ some character codes greater than 128 are used for accented letters_ and these are matched by _w__ -- - Richard Quadling "Standing on the shoulders of some very clever giants!" EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Stripping Characters
On Tue, Jun 22, 2010 at 9:40 AM, Rick Dwyer wrote: > Hello List. > > I need to remove characters from a string and replace them with and > underscore. > > So instead of having something like: > > $moditem = str_replace("--","_","$mystring"); > $moditem = str_replace("?","_","$mystring"); > $moditem = str_replace("!","_","$mystring"); > etc. > > For every possible character I can think of, is there a way to simply omit > any character that is not an alpha character and not a number value from 0 > to 9? > check the docs, the first parameter may be an array of multiple needles, e.g. $moditem = str_replace(array('-', '?', '!'), '_', $mystring); you could likely do something more elegant w/ preg_replace() tho. -nathan
Re: [PHP] Stripping Characters
On 22 June 2010 16:44, Ashley Sheridan wrote: > On Tue, 2010-06-22 at 11:40 -0400, Rick Dwyer wrote: > >> Hello List. >> >> I need to remove characters from a string and replace them with and >> underscore. >> >> So instead of having something like: >> >> $moditem = str_replace("--","_","$mystring"); >> $moditem = str_replace("?","_","$mystring"); >> $moditem = str_replace("!","_","$mystring"); >> etc. >> >> For every possible character I can think of, is there a way to simply >> omit any character that is not an alpha character and not a number >> value from 0 to 9? >> >> >> --Rick >> >> >> > > > Use preg_replace(), which allows you to use a regex to specify what you > want to match: > > $find = '/[^a-z0-9]/i'; > $replace = '_'; > $new_string = preg_replace($find, $replace, $old_string); > > Thanks, > Ash > http://www.ashleysheridan.co.uk > > > Watch out for white space in there. Tabs, spaces, new lines, etc. will also be converted to underscore. $find = '/[^\w\s]/i'; [^\w\s] Match a single character NOT present in the list below «[^\w\s]» A word character (letters, digits, and underscores) «\w» A whitespace character (spaces, tabs, and line breaks) «\s» "A "word" character is any letter or digit or the underscore character, that is, any character which can be part of a Perl "word". The definition of letters and digits is controlled by PCRE's character tables, and may vary if locale-specific matching is taking place. For example, in the "fr" (French) locale, some character codes greater than 128 are used for accented letters, and these are matched by \w." -- - Richard Quadling "Standing on the shoulders of some very clever giants!" EE : http://www.experts-exchange.com/M_248814.html EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 ZOPA : http://uk.zopa.com/member/RQuadling -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Stripping Characters
Perhaps, ereg_replace("your regex", "replacement_string", String $variable). Regards, Shreyas On Tue, Jun 22, 2010 at 9:10 PM, Rick Dwyer wrote: > Hello List. > > I need to remove characters from a string and replace them with and > underscore. > > So instead of having something like: > > $moditem = str_replace("--","_","$mystring"); > $moditem = str_replace("?","_","$mystring"); > $moditem = str_replace("!","_","$mystring"); > etc. > > For every possible character I can think of, is there a way to simply omit > any character that is not an alpha character and not a number value from 0 > to 9? > > > --Rick > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Regards, Shreyas Agasthya
Re: [PHP] Stripping Characters
On Tue, 2010-06-22 at 11:40 -0400, Rick Dwyer wrote: > Hello List. > > I need to remove characters from a string and replace them with and > underscore. > > So instead of having something like: > > $moditem = str_replace("--","_","$mystring"); > $moditem = str_replace("?","_","$mystring"); > $moditem = str_replace("!","_","$mystring"); > etc. > > For every possible character I can think of, is there a way to simply > omit any character that is not an alpha character and not a number > value from 0 to 9? > > > --Rick > > > Use preg_replace(), which allows you to use a regex to specify what you want to match: $find = '/[^a-z0-9]/i'; $replace = '_'; $new_string = preg_replace($find, $replace, $old_string); Thanks, Ash http://www.ashleysheridan.co.uk
RE: [PHP] Stripping Characters
Hello, can this resolve your problem? $trans = array( "from" => "to", "another" => "to"); $moditem = StrTr($moditem, $trans); -- http://cz.php.net/manual/en/function.strtr.php David -Original Message- From: Rick Dwyer [mailto:rpdw...@earthlink.net] Sent: Tuesday, June 22, 2010 5:41 PM To: php-general@lists.php.net Subject: [PHP] Stripping Characters Hello List. I need to remove characters from a string and replace them with and underscore. So instead of having something like: $moditem = str_replace("--","_","$mystring"); $moditem = str_replace("?","_","$mystring"); $moditem = str_replace("!","_","$mystring"); etc. For every possible character I can think of, is there a way to simply omit any character that is not an alpha character and not a number value from 0 to 9? --Rick -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Stripping characters.....
Clarification: So really, what you want to achieve is to ONLY have the email address? I'm POSITIVE there's a better way with ereg_replace(), but I haven't got time to experiment, and i'm no expert :) So, what I figured was that you would loop through the $email, and if the first char wasn't a "<", strip it out: "Chris Ditty" <[EMAIL PROTECTED]> Chris Ditty" <[EMAIL PROTECTED]> hris Ditty" <[EMAIL PROTECTED]> ris Ditty" <[EMAIL PROTECTED]> is Ditty" <[EMAIL PROTECTED]> ... <[EMAIL PROTECTED]> You're then left with ", you'll now have <[EMAIL PROTECTED]>. Then, you want to strip out the leading < and the trailing > only (this means weird email address' which have a < or > in them won't break. You should now have [EMAIL PROTECTED] UNTESTED code: otsweeps.com>'; while (!eregi('^<', $email)) { $email = substr($email, 1); //echo htmlspecialchars($email).""; } $email = ereg_replace("^<", "", $email); $email = ereg_replace(">$", "", $email); echo htmlspecialchars($email).""; ?> Like I said, this is probably NOT the best solution, just *a* solution, but it should work. It's independent of the "'s you had in there... all it cares about is $email ending with "", getting rid of the preceeding it, and then getting rid of the <>'s that wrap it. You might also like to get rid of anything trailing the >. Uncomment line 6 to watch the progress of the while loop :) Justin French Creative Director http://Indent.com.au on 28/04/02 4:16 PM, CDitty ([EMAIL PROTECTED]) wrote: > Hello all, > > Does anyone have any snippets of code that will strip several characters > from a string? I am trying to figure out how to do this without using 3 > different if statement blocks. This is what I am looking for. > > ieUser email address is "Chris Ditty" > <[EMAIL PROTECTED]> or Chris Ditty <[EMAIL PROTECTED]> > > I need to be able to strip the quotes and everything between them and the > < > signs from the first one and then the name and the < > signs from the > second. > > Does anyone know how to do this quickly and easily? > > Thanks > > CDitty > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php