Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #1366

Re: preg_replace help

Date 2011-05-02 11:44 -0400
From bill <nobody@spamcop.net>
Newsgroups comp.lang.php
Subject Re: preg_replace help
References <oNidnfTM6dv7BCPQnZ2dnUVZ_gydnZ2d@cablespeedmi.com> <ipm7ge$l2m$1@dont-email.me>
Message-ID <q8OdnQ-VD_bxTCPQnZ2dnUVZ_tGdnZ2d@cablespeedmi.com> (permalink)

Show all headers | View raw


On 5/2/2011 8:19 AM, Jerry Stuckle wrote:
> On 5/2/2011 7:45 AM, bill wrote:
>> unfortunately the documentation, while telling one how to use
>> preg_replace, does not tell how to write a regular expression.
>>
>> In this case google is not my friend as the tutorial I read
>> suggested
>> (as _I_ read it):
>>
>> $contact = preg_replace("[0-9\-]*","",$contact);
>>
>> when I want to return a string that contains only numbers or
>> the hyphen
>> (a telephone number in the US).
>>
>> preg_replace does not like the * which the tutorial says means
>> "do it
>> over and over"
>>
>> and $contact = preg_replace("[0-9\-]","",$contact);
>> doesn not modify the string at all.
>>
>> a bit of help (or a lot of help) would be appreciated (no, I
>> have not
>> been in school for quite some time, Jerry)
>>
>> bill
>
> You're close, Bill, but you have a couple of problems.
>
> First of all, the PHP preg_xxx() functions require a separator
> character around the search string. Stupid, I know, but then
> there are a lot of stupid things in PHP.
>
> To get rid of your error, you need something like:
>
> $contact = preg_replace("/[0-9\-]*/","",$contact);
>
> However, this will replace all digits and hyphens with a null
> string - just the opposite of what I think you need to do. So you
> need to negate the character class, i.e.
>
> $contact = preg_replace("/[^0-9\-]*/","",$contact);
>
> Should get you what you want.
>
> However, if you're trying to "clean up" user input, I recommend
> you not do this. Rather, check to see if there are any invalid
> characters in the input, and if so, return an error message to
> the user. Nothing is quite so annoying as accidentally hitting
> "y" instead of "6" and not noticing it when you type it in - and
> having the "y" just dropped from the number, leaving an invalid
> phone number.

Actually, this is from an internal database where the phone 
number may be qualified, as in "cell: 123-456-7890"
>
> And BTW - I don't know why you would think I would take this as a
> homework problem.
'cus sometimes people do that.

Many thanks to you and to Alvaro for each contributing to the 
answer and to my education.

BTW, Alvaro,
The PHP documentation may be terrific, but I just can't find it.
When I search for "regular expression" or "regexp"  I get links 
to the functions (which do not explain writing a regexp)

  or to lots of things I see no relationship.

bill

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

preg_replace help bill <nobody@spamcop.net> - 2011-05-02 07:45 -0400
  Re: preg_replace help Jerry Stuckle <jstucklex@attglobal.net> - 2011-05-02 08:19 -0400
    Re: preg_replace help bill <nobody@spamcop.net> - 2011-05-02 11:44 -0400
      Re: preg_replace help "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2011-05-02 18:51 +0200
        Re: preg_replace help bill <nobody@spamcop.net> - 2011-05-04 06:41 -0400
    Re: preg_replace help Michael Fesser <netizen@gmx.de> - 2011-05-02 18:05 +0200
  Re: preg_replace help "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2011-05-02 16:19 +0200

csiph-web