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
Switch to threaded view of this topic Create a new topic Submit Reply
preg_replace help [message #173734] Mon, 02 May 2011 11:45 Go to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
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
Re: preg_replace help [message #173735 is a reply to message #173734] Mon, 02 May 2011 12:19 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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(at)attglobal(dot)net
==================
Re: preg_replace help [message #173736 is a reply to message #173734] Mon, 02 May 2011 14:19 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 02/05/2011 13:45, bill escribió/wrote:
> unfortunately the documentation, while telling one how to use
> preg_replace, does not tell how to write a regular expression.

Well, it does... It has a few examples:

http://es.php.net/manual/en/pcre.examples.php

.... as well as full syntax reference:

http://es.php.net/manual/en/pcre.pattern.php


> In this case google is not my friend as the tutorial I read suggested

It's a pity that such tutorial does not simply point you to the official
documentation. The PHP manual is outstanding :)


> (as _I_ read it):
>
> $contact = preg_replace("[0-9\-]*","",$contact);

This is not valid. A regular expression needs to have a delimiter:

http://es.php.net/manual/en/regexp.reference.delimiters.php

Your double quotes do not count as delimiters because they're already
being used to type the string. It's a restriction imposed by the PHP
engine: you cannot type a regexp as is, it needs to be inside a string.
The regexp library will only see this:

[0-9\-]*

So you possibly want something like this:

$contact = preg_replace('/[0-9\-]*/', '', $contact);

In this case, it doesn't really matter whether you use single or double
quotes, but you should not that they are not equivalent:

http://es.php.net/manual/en/language.types.string.php

It's also worth noting that error messages are there to help you. You
didn't bother copying the error here so it's likely that you didn't pay
much attention to it. Always read them carefully! When I run your
original code I get this:

Warning: preg_replace() Unknown modifier '*'

The information we get is that PHP believes that "*" is a modifier:

http://es.php.net/manual/en/reference.pcre.pattern.modifiers.php

You didn't intend to use it as modifier, did you? And modifiers go after
the end delimiter. That tells us that there's something wrong with your
delimiters.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: preg_replace help [message #173737 is a reply to message #173735] Mon, 02 May 2011 15:44 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
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
Re: preg_replace help [message #173738 is a reply to message #173735] Mon, 02 May 2011 16:05 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(Jerry Stuckle)

> 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.

It avoids ambiguities when using modifiers, which are noted after the
delimiter. Other languages do it the same way, so it's nothing you can
blame PHP for.

Micha
Re: preg_replace help [message #173743 is a reply to message #173737] Mon, 02 May 2011 16:51 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 02/05/2011 17:44, bill escribió/wrote:
> 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)

In such cases, I suggest you start from the function you plan to use
(e.g. preg_match) and follow the navigation links in the left column.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: preg_replace help [message #173777 is a reply to message #173743] Wed, 04 May 2011 10:41 Go to previous message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 5/2/2011 12:51 PM, "Álvaro G. Vicario" wrote:
> El 02/05/2011 17:44, bill escribió/wrote:
>> 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)
>
> In such cases, I suggest you start from the function you plan to
> use (e.g. preg_match) and follow the navigation links in the left
> column.
>
>
Good idea, I didn't think to look up the column to the higher
pages. Thanks.

bill
  Switch to threaded view of this topic Create a new topic Submit Reply
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 16:38:03 GMT 2024

Total time taken to generate the page: 0.02837 seconds