Re: [PHP] Unable to match dollar sign in preg_match

2002-12-24 Thread Sean Burlington
John W. Holmes wrote:

I don't know. I just got that from reading the manual. The very first
user comment on the preg_match page.


to put a literal dollar sign in a regex it has to be escaped \$

to put a backslash in a double quoted string you have to escape it \\$

in order to put a literal dollar sign in a double quoted string you have 
to escape it \\\$

or you can use sinlgle quotes - where you only have to escape the dollar 
sign once to mark it a a literal dollar sign and not an end string/line 
placeholder

preg_match('/\$example/' , $matchme))

Sean



chrisbolt at home dot com
12-Mar-2000 03:23 
 
If you want to use some of PHP's special characters in your expression,
such as $, you must escape it twice. For example: 

$matchme = "\$example"; 
if (preg_match("/\$example/", $matchme)) { 

will not be matched because PHP interprets the \$ and passes it as $.
Instead, you must do this: 

if (preg_match("/\\\$example/", $matchme)) {
---

Don't forget to check the manual.

---John W. Holmes...

PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/


-Original Message-
From: Randall Perry [mailto:[EMAIL PROTECTED]]
Sent: Monday, December 23, 2002 4:07 PM
To: [EMAIL PROTECTED]
Subject: Re: [PHP] Unable to match dollar sign in preg_match

Hokay, that works, but what's with the triple backslashes?




preg_match ("/^.*_\%split\%_$(\d*\.\d*)/", $data, $match);


Should be

preg_match ("/^.*_\%split\%_\\\$(\d*\.\d*)/", $data, $match);

---John W. Holmes...

PHP Architect - A monthly magazine for PHP Professionals. Get your


copy


today. http://www.phparch.com/




--
Randall Perry
sysTame

Xserve Web Hosting/Co-location
Website Development/Promotion
Mac Consulting/Sales

http://www.systame.com/



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php










--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Unable to match dollar sign in preg_match

2002-12-24 Thread Ford, Mike [LSS]
> -Original Message-
> From: John W. Holmes [mailto:[EMAIL PROTECTED]]
> Sent: 24 December 2002 00:44
> 
> I don't know. I just got that from reading the manual. The very first
> user comment on the preg_match page.

Sorry, misread this before and took the explanation below as John replying to 
chrisbolt -- but actually, it's John *quoting* chrisbolt, isn't it?  In which case, 
I'll chip in further with:

> 
> chrisbolt at home dot com
> 12-Mar-2000 03:23 
>  
> If you want to use some of PHP's special characters in your 
> expression,
> such as $, you must escape it twice. For example:

$ is both a PHP special character in double-quoted strings, and a special character in 
preg pattern matches (meaning "start of string or line") -- so it first has to be 
escaped for preg, giving \$, and then the \ and the $ each have to be escaped for the 
PHP double-quoted string giving "\\\$".

Basically, if you put your pattern in a double-quoted string, every time you want to 
pass a \ to preg (say as an escape for a special character such as ., or as part of a 
character class such as \d) you have to assess whether it needs escaping for PHP, 
leading to a double backslash in the PHP string.  The safest strategy is just to 
double it every time, as this will always be de-escaped by PHP to a single \ in the 
string which is then passed to preg.

If you use single-quoted strings for your preg pattern, you don't get this problem as 
the only character that needs escaping for PHP is then the single quote itself!

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Unable to match dollar sign in preg_match

2002-12-24 Thread Ford, Mike [LSS]
> -Original Message-
> From: John W. Holmes [mailto:[EMAIL PROTECTED]]
> Sent: 24 December 2002 00:44
>  
> If you want to use some of PHP's special characters in your 
> expression,

Don't you mean "preg's special characters"? Or even "PHP special characters which are 
also preg special characters", as it's this duality of specialness that leads to the 
necessity of double-escaping!

> such as $, you must escape it twice. For example: 
> 
> $matchme = "\$example"; 
> if (preg_match("/\$example/", $matchme)) { 
> 
> will not be matched because PHP interprets the \$ and passes it as $.
> Instead, you must do this: 
> 
> if (preg_match("/\\\$example/", $matchme)) {

This is the main reason I prefer to use *single*-quoted strings whenever possible in 
preg_ calls -- the above could more legibly be written as:

if (preg_match('/\$example/', $matchme)) {

This is an instance where I'd even tend to prefer to write single-quoted strings 
concatenated to bare variables, rather than using double-quoted strings and 
variable-interpolation:

if (preg_match('/\$'.$example.'\./', $matchme)) {

rather than

if (preg_match("/\\\$$example\\./", $matchme)) {

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Unable to match dollar sign in preg_match

2002-12-23 Thread John W. Holmes
I don't know. I just got that from reading the manual. The very first
user comment on the preg_match page.


chrisbolt at home dot com
12-Mar-2000 03:23 
 
If you want to use some of PHP's special characters in your expression,
such as $, you must escape it twice. For example: 

$matchme = "\$example"; 
if (preg_match("/\$example/", $matchme)) { 

will not be matched because PHP interprets the \$ and passes it as $.
Instead, you must do this: 

if (preg_match("/\\\$example/", $matchme)) {
---

Don't forget to check the manual.

---John W. Holmes...

PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/

> -Original Message-
> From: Randall Perry [mailto:[EMAIL PROTECTED]]
> Sent: Monday, December 23, 2002 4:07 PM
> To: [EMAIL PROTECTED]
> Subject: Re: [PHP] Unable to match dollar sign in preg_match
> 
> Hokay, that works, but what's with the triple backslashes?
> 
> 
> >> preg_match ("/^.*_\%split\%_$(\d*\.\d*)/", $data, $match);
> >
> > Should be
> >
> > preg_match ("/^.*_\%split\%_\\\$(\d*\.\d*)/", $data, $match);
> >
> > ---John W. Holmes...
> >
> > PHP Architect - A monthly magazine for PHP Professionals. Get your
copy
> > today. http://www.phparch.com/
> >
> >
> 
> --
> Randall Perry
> sysTame
> 
> Xserve Web Hosting/Co-location
> Website Development/Promotion
> Mac Consulting/Sales
> 
> http://www.systame.com/
> 
> 
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Unable to match dollar sign in preg_match

2002-12-23 Thread Randall Perry
Hokay, that works, but what's with the triple backslashes?


>> preg_match ("/^.*_\%split\%_$(\d*\.\d*)/", $data, $match);
> 
> Should be
> 
> preg_match ("/^.*_\%split\%_\\\$(\d*\.\d*)/", $data, $match);
> 
> ---John W. Holmes...
> 
> PHP Architect - A monthly magazine for PHP Professionals. Get your copy
> today. http://www.phparch.com/
> 
> 

-- 
Randall Perry
sysTame

Xserve Web Hosting/Co-location
Website Development/Promotion
Mac Consulting/Sales

http://www.systame.com/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Unable to match dollar sign in preg_match

2002-12-23 Thread John W. Holmes
> preg_match ("/^.*_\%split\%_$(\d*\.\d*)/", $data, $match);

Should be

preg_match ("/^.*_\%split\%_\\\$(\d*\.\d*)/", $data, $match);

---John W. Holmes...

PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php