FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » preg_replace help
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: preg_replace help [message #173737 is a reply to message #173735] Mon, 02 May 2011 15:44 Go to previous messageGo to previous message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma:
Senior Member
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: How to extract value using php soapclient
Next Topic: Upgraded -- But Still Showing Old PHP Version
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 15:27:41 GMT 2024

Total time taken to generate the page: 0.05304 seconds