You are here: Re: [PHP] preg_match « PHP « IT news, forums, messages
Re: [PHP] preg_match

Posted by "Richard Lynch" on 08/24/05 10:29

On Tue, August 23, 2005 1:58 am, Robin Vickery wrote:
> On 8/23/05, Richard Lynch <ceo@l-i-e.com> wrote:
>> The advantage that when I have to change the string to match the
>> new/altered data, and that data has, oh, I dunno, something like '
>> or
>> \ or 0, 1, 2, 3, ...9 in it, and then my Regex suddenly doesn't work
>> because \abc7 won't work the same as \7abc but \\abc7 works the same
>> as \\7abc
>
> Ummm... no it doesn't.
>
> Both of these will fail to compile:
> preg_match( '/\\7abc/', $myString);
> preg_match( '/\7abc/', $myString);

Well, duh!

You'd have to use them correctly in Regular Expressions to not get a
"failure to compile" from the PCRE Module.

So you'd have to have a Regex matching thingie with 7 (positional)
(parameters) and be using preg_replace with the "\\7abc" as the
*SECOND* argument to get a valid Regular Expression usage.

At which point you will RAPIDLY find a *BIG* difference between
"\\7abc" and "\7abc"

But you're *NOT* seeing a PHP syntax error -- You're seeing an error
in the Regular Expression grammer. Which means it's got *NOTHING* to
do with the topic at hand of \ or \\ inside of PHP strings.

You might as well point to an SQL syntax error such as "SELECT \\ FROM
tablename";

It would be equally relevant to the topic here.

Look, let's just forget the Regex, since it's confusing you.

Take it out of the picture entirely.

------------------------

The following statements are General Principles in PHP, and have
NOTHING to do with PCRE, POSIX RegEx, or the phases of the moon:

"\\7abc" and "\7abc" are NOT the same string.
"\\abc7" and "\abc7" *ARE* the same string.


If you use \\ it *ALWAYS* works.
If you use \ it *SOMETIMES* works.


*SOMETIMES* \ means just \ and *SOMETIMES* it means something else
based on the next character.

\\ *ALWAYS* means \ Period.


Even in apostrophes (aka single quote) \\ always means \

\ sometimes means \ and sometimes (only 2 cases) it doesn't mean \

'contrived \'example' Kosher
'contrived \\'example' Will PHP see \\ first or \' first? BAD
'contrived \\\'example' Kosher
'contrived \'\example' Syntactically correct, but Bad Practice
'contrived \'\\example' Kosher


If you use \ to mean \ then sooner or later, you're going to end up
changing the character after the \ to something that suddenly changes
the meaning of \ to not be \ any more but to be some special character
(newline, tab, etc)

If you use \\ to mean \ then it just ALWAYS WORKS, no matter what you
put after it.

Always Works.

Sometimes Works.

You decide which one you like better.

This is especially important when you start getting your Regex strings
from external sources such as User Input or outside data.

If you can't be certain that their string doesn't contain \t as
opposed to \\t, then you'd better be in the habit of using \\
consistently.

Otherwise you're going to forget to do it in your code to handle their
input, and it's going to be a bug that you'll waste a lot of time
tracking down when somebody puts in \t a year after you wrote that
code, and it's now surrounded by 10,000 more lines of code, and you
get a bug report about a feature/module that's been working flawlessly
for years.

Use \\ consistently to mean \ in PHP strings, or you WILL regret it,
sooner or later.

Yes, the manual definition specifically allows you to use \ and any
non-special character afterwards. That still doesn't make it a Good
Practice.

The manual no longer specifies that you can indent your code badly,
AFAICS, which is probably just as well, but you *can* do it, and it's
syntactically correct. And it's a Bad Practice.

This is MY OPINION.

Obviously you disagree, or at least did yesterday.

If you still disagree, I give up.

Write your PHP in whatever style you want.

--
Like Music?
http://l-i-e.com/artists.htm

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация