hmbscully has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm attempting to convert a website from SSIs to PHP includes.

This is the perl and regex I'm using:

perl -pi -e 's|<!--#include virtual="/ssi/footer.txt"-->|<?php include($_SERVER['DOCUMENT_ROOT'].'/ssi/footer.txt'); ?>|g'

Which matches and replaces the SSI with <?php include(./ssi/edfooter.txt); ?> losing the '$_SERVER['DOCUMENT_ROOT'].'' in translation.

I understand that the parens and the $ are causing the problems and things need escaped, but I've tried everything I can think of to try and escape that part. Each time I escape one thing or quote one part another way, it seems that there is another part of the expression that then can't be quoted. I'm not sure this match is even possible?

thanks!


I learn more and more about less and less until eventually I know everything about nothing.

Replies are listed 'Best First'.
Re: regex question with balanced text
by Skeeve (Parson) on Aug 28, 2007 at 15:55 UTC
    Not only the $ but also the '
    perl -pi <<'PRG_END' s|<!--#include virtual="/ssi/footer.txt"-->|<?php include(\$_SERVER['D +OCUMENT_ROOT'].'/ssi/footer.txt'); ?>|g; PRG_END

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
      If I use the here doc, how do I use this in conjunction with xargs? I thought that required it to be a one-liner?

      find /home/site/htdocs -name "*.html" | xargs -n 1 perl -pi <<'PRG_END +' s|<!--#include virtual="/ssi/footer.txt"-->|<?php include(\$_SERVER['D +OCUMENT_ROOT'].'/ssi/footer.txt'); ?>|g; PRG_END

      Thows errors like this:

      Can't open perl script "s|<!--#include": No such file or directory. Use -S to search $PATH for it. Can't open perl script "virtual=/ssi/edfooter.txt-->|<?php": No such f +ile or directory. Use -S to search $PATH for it. Can't open perl script "include($_SERVER[DOCUMENT_ROOT]./ssi/edfooter. +txt);": No such file or directory. Use -S to search $PATH for it. Can't open perl script "?>|g;": No such file or directory. Use -S to search $PATH for it.

      I learn more and more about less and less until eventually I know everything about nothing.
Re: regex question with balanced text
by Codon (Friar) on Aug 28, 2007 at 19:43 UTC
    Your shell is getting you. You could put your one-liner into a file and specify that filename rather than the -e 'string' in the xargs command.
    perl -pi ssi2php.pl

    Ivan Heffner
    Sr. Software Engineer
    WhitePages.com, Inc.