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


Groups > comp.lang.php > #1363

Re: preg_replace help

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: preg_replace help
Date 2011-05-02 08:19 -0400
Organization A noiseless patient Spider
Message-ID <ipm7ge$l2m$1@dont-email.me> (permalink)
References <oNidnfTM6dv7BCPQnZ2dnUVZ_gydnZ2d@cablespeedmi.com>

Show all headers | View raw


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.

And BTW - I don't know why you would think I would take this as a 
homework problem.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

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