[PHP] Re: help using ereg_replace()

2002-01-14 Thread liljim

Hello Erik,

 Hello everyone,

 I am trying to learn how to use ereg_replace(), but I don't seem to be
 doing it correctly.

 $phone is a 10-digit string.

 I want to perform the following:

 $phone = ereg_replace(.{10}, [part where I am confused], $phone);

 I would like the output to be

 $phone = (555) 555-;

I would use preg_replace to do this.

?

$phone = 55;

$phone = preg_replace(/([0-9]{3,3})([0-9]{3,3})([0-9]+)/, ($1) $2-$3,
$phone);

echo $phone;

?

The parentheses in the first part of the expression capture what you want.
The stuff between the [and] is a characterclass (in this instance 0 through
9).
The stuff between the curly braces is min,max.

So.
([0-9]{3,3}) Match 0 through 9 a minimum (and max) of three times
([0-9]+) - match at least once.

Hence the number becomes:
($1) $2-$3
($1) = stuff in first parentheses (three digits) - this case, (555)
followed by a space
followed by the next three digits
followed by a dash
followed by whatever's left.
Note that in the second part of the expression, parentheses lose their
special meaning, and can be used as regular characters (as can the other
metacharacters).

You will want to make sure that the numbers when inputted are in the format
you want them when you bring them back out of the file / db whatever -

if (!preg_match(/^[0-9]{10,10}/, $input)) {
// We need 10 numbers!
}

Hope that helps.

James




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Re: help using ereg_replace()

2002-01-14 Thread Erik Price

liljim,

Thanks for the advice (and especially the explanation!) -- I note that 
you chose the Perl regexp engine rather than the e regexp engine 
(POSIX?).  Quite a few people recommended O'Reilly's Mastering Regular 
Expressions over the weekend.  Does anyone know if it covers the Perl 
syntax*?  This seems like a good book to learn more.

Erik
* I don't know Perl


On Monday, January 14, 2002, at 11:11  AM, liljim wrote:

 I would use preg_replace to do this.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Re: help using ereg_replace()

2002-01-14 Thread liljim

Hi Erik,

 Thanks for the advice (and especially the explanation!) -- I note that
 you chose the Perl regexp engine rather than the e regexp engine
 (POSIX?).  Quite a few people recommended O'Reilly's Mastering Regular
 Expressions over the weekend.  Does anyone know if it covers the Perl
 syntax*?  This seems like a good book to learn more.

The book (though going into examples using perl mainly) gives you a general
grounding in regular expressions - I learned php before perl, and
javascript, though the book has equipped me equally well for all three.  I
love it, and would recommend it for anyyone who's even vaguely interested in
regexes.

James


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




RE: [PHP] Re: help using ereg_replace()

2002-01-14 Thread Bart Brinkmann

I own and have read Mastering Regular Expressions (excellent book). I've
programmed perl for 3 years and have recently started migrating one of my
projects into php. I can't stand ereg, I prefer preg. While functions such
as substitute and match differ in syntax from perl to php, this book will
teach you quite a bit about how to effectively use preg, which i've found to
be easier, and faster than ereg.

For the record, I like perl's syntax better...   ;p

Hope this helps,

/bart

-Original Message-
From: Erik Price [mailto:[EMAIL PROTECTED]]
Sent: Monday, January 14, 2002 11:26 AM
To: liljim
Cc: [EMAIL PROTECTED]
Subject: Re: [PHP] Re: help using ereg_replace()


liljim,

Thanks for the advice (and especially the explanation!) -- I note that
you chose the Perl regexp engine rather than the e regexp engine
(POSIX?).  Quite a few people recommended O'Reilly's Mastering Regular
Expressions over the weekend.  Does anyone know if it covers the Perl
syntax*?  This seems like a good book to learn more.

Erik
* I don't know Perl


On Monday, January 14, 2002, at 11:11  AM, liljim wrote:

 I would use preg_replace to do this.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]