[pmwiki-users] Need help writing a simple markup for IP addresses inside of pages

Patrick R. Michaud pmichaud at pobox.com
Mon Oct 16 17:45:40 CDT 2006


On Mon, Oct 16, 2006 at 05:26:04PM -0500, Chris Cox wrote:
> Patrick R. Michaud wrote:
> > Similarly, do:
> > 
> >     Markup('IPRevAddy', '<IPAddy',
> >       '/\\b(?<!\\|)(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)\\.in-addr\\.arpa\\b/',
> >       '[[Network/Ip$4-$3-$2-$1|$0]]');
> > 
> > Note that this second rule is set to be processed _before_ the 'IPAddy'
> > rule, so that in-addr.arpa addresses are processed before the standard
> > IP addresses.
> 
> You're the second person saying I need to escape the backslash.  Why?
> It seems to work without escaping it.  Thanks for the tips.

You probably don't _need_ to escape the backslash, but PHP has
some odd rules about what it does with backslashes, so by way of
defensive programming I always escape my backslashes when I want them
to be passed through to preg_*.

In particular, note the difference between:

    print 'Hello \' world';       # outputs "Hello ' world"
    print "Hello \' world";       # outputs "Hello \' world"  (!)

Unlike C, Perl, Java, and just about every other language I
commonly use, where a backslash is always an escape, in PHP a 
backslash in double-quoted strings is a normal backslash character 
_unless_ it's followed by something that makes it into an escape.
And as demonstrated above, \' in a double quoted string becomes
backslash+quote, whereas C or Perl would treat it as just a quote.

I got tried of remembering when I needed to escape my backslashes,
and also ran into a few problems when changing single-quoted
strings into double-quoted strings would suddenly cause backslashes
to appear, so I just adopted a personal policy of always escaping
backslashes in strings when my intent is that there be a single
backslash in the final string.

Pm




More information about the pmwiki-users mailing list