Re: preg_replace help [message #173736 is a reply to message #173734] |
Mon, 02 May 2011 14:19 |
alvaro.NOSPAMTHANX
Messages: 277 Registered: September 2010
Karma:
|
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
--
|
|
|