RE: Anyone have a tool/script to convert <? to <?php (but not <?=)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




> -----Original Message-----
> From: Jim Lucas [mailto:lists@xxxxxxxxx]
> Sent: Thursday, February 20, 2014 2:10 PM
> To: Daevid Vincent; php-general@xxxxxxxxxxxxx
> Subject: Re:  Anyone have a tool/script to convert <? to <?php (but not
> <?=)
> 
> On 02/20/2014 02:05 PM, Jim Lucas wrote:
> > On 02/20/2014 11:34 AM, Daevid Vincent wrote:
> >> I've started writing the tool (which I will post when finished for others
> to
> >> use).
> >>
> >> However, I have a few questions about preg_replace():
> >>
> >> How do I search for  '$' literal? I thought it was just backslash escape
> it
> >> but that isn't matching (as indicated by the semi-colon in the output)
> >>
> 
> also, the thing to watch out for when trying to match a $ in your regex, if
> you use double quotes, you need to double escape the $.  If you use single
> quotes, you only need 1 backslash.  So...
> 
> "/\\\$/"   or   '/\$/'  would be the same
> 
> >> php > echo preg_replace("/<\?=(\$.+?);?\s*\?/", '<?= '.trim("$1").' ?>',
> >> 'Username: <?=    $username;   ?>')."\n";
> >> Username: <?=    $username;   ?>
> >>
> >> For fun I tried without the \$ and just $ but same thing.
> >>
> >> php > echo preg_replace("/<\?=($.+?);?\s*\?/", '<?= '.trim("$1").' ?>',
> >> 'Username: <?=    $username;   ?>')."\n";
> >> Username: <?=    $username;   ?>
> >>
> >> Taking off the $ check all together I get the match, but then how can I
> >> apply trim() to the match? The documentation seems to imply this should
> work
> >> no? http://php.net/preg_replace
> >>
> >> php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.trim("$1").' ?',
> >> 'Username: <?=    $username;   ?>')."\n";
> >> Username: <?=     $username ?>
> >>
> >> For sanity I tried a few other things and it doesn't appear that the
> chosen
> >> function is processing the match variable $1?
> >>
> >> php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.trim('$1').' ?',
> >> 'Username: <?=    $username;   ?>')."\n";
> >> Username: <?=     $username ?>
> >>
> >> php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5('$1').' ?',
> >> 'Username: <?=    $username;   ?>')."\n";
> >> Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
> >>
> >> php > echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5('$1').' ?',
> >> 'Username: <?=    $foo;   ?>')."\n";
> >> Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
> >>
> >> echo preg_replace("/<\?=(.+?);?\s*\?/", '<?= '.md5("$1").' ?', 'Username:
> >> <?=    $foo;   ?>')."\n";
> >> Username: <?= 06d3730efc83058f497d3d44f2f364e3 ?>
> >>
> >> Notice how the MD5 didn't change, indicating that it's MD5'ing the
> literal
> >> string '$1' and verified by this
> >>
> >> php > echo md5('$1')."\n";
> >> 06d3730efc83058f497d3d44f2f364e3
> >>
> >> *sigh*
> >>
> >> Ultimately I was able to craft a regex that I think will work in this
> instance
> >>
> >> php > echo preg_replace("/<\?=\s*(.+?);?\s*\?/", "<?= $1 ?", 'Username:
> >> <?=    $username;   ?>')."\n";
> >> Username: <?= $username ?>
> >>
> >> But I'd like to know the answers to the two questions above for future
> >> reference
> >>
> >>
> >
> >
> > Try this.  It works for me.  If the examples don't cover all your test
> cases,
> > please include them when you respond.
> >
> > [jlucas@jim ~]$ cat junk.php
> > <?php
> >
> > $lines[] = 'Username: <?=    $username;   ?>';
> > $lines[] = 'Username: <?= $username; ?>';
> > $lines[] = 'Username: <?=$username; ?>';
> > $lines[] = 'Username: <?= $username;?>';
> > $lines[] = 'Username: <?=$username;?>';
> >
> > $lines[] = 'Username: <?=    $username   ?>';
> > $lines[] = 'Username: <?= $username ?>';
> > $lines[] = 'Username: <?=$username ?>';
> > $lines[] = 'Username: <?= $username?>';
> > $lines[] = 'Username: <?=$username?>';
> >
> >
> > foreach ( $lines AS $line ) {
> >    echo preg_replace(
> >              '/<\?=\s*(\$[\w\d_]+);?\s*\?>/',
> >              '<?= '.trim('$1').' ?>',
> >              $line)."\n";
> > }
> >
> >
> >
> > [jlucas@jim ~]$ php -f junk.php
> > Username: <?= $username ?>
> > Username: <?= $username ?>
> > Username: <?= $username ?>
> > Username: <?= $username ?>
> > Username: <?= $username ?>
> > Username: <?= $username ?>
> > Username: <?= $username ?>
> > Username: <?= $username ?>
> > Username: <?= $username ?>
> > Username: <?= $username ?>
> >

Thanks for the help Jim. I modified it a bit to handle more valid variable cases...

$line = preg_replace('/<\?=\s*(\$[\w\d_\'\"\[\]\$]+);?\s*\?>/', '<?= $1 ?>', $line);

The trim() is still not working right. You can change it to MD5() and see that the value doesn't change.
While not needed in this particular case/regex, I'm curious why or more importantly how to process the matched part?

-------------------------- 8< ----------------------
<?
   if ($foo_bar['test'])
?>

bar = <?=$foo;?>
bar = <?=   $foo;    ?>
bar = <?=$foo?>
bar = <?=   $foo    ?>

<?= $foo_bar['test'] ?>
<?=$foo_bar['test'];?>
<?= $foo_bar["test"] ?>
<?=$foo_bar["test"];?>
<?= $foo_bar[0] ?>
<?=$foo[$bar];?>

<?php define('DEFINED_CONSTANT','SOME DEFINITION HERE'); ?>
<?= DEFINED_CONSTANT ?>
<?= UNDEFINED_CONSTANT ?>

<? if($foo_bar['test']) ?>
<?php $foo_bar['test']='bar'; ?>
Username: <?=    $foo_bar['username'];    ?>
Password: <?=    $foo_bar['password']    ?>
<?=$foo_bar['test']?>
<?echo 'i told you so';?>

-------------------------- >8 ----------------------

Results in :

<?
   if ($foo_bar['test'])
?>

bar = <?= $foo ?>
bar = <?= $foo ?>
bar = <?= $foo ?>
bar = <?= $foo ?>

<?= $foo_bar['test'] ?>
<?= $foo_bar['test'] ?>
<?= $foo_bar["test"] ?>
<?= $foo_bar["test"] ?>
<?= $foo_bar[0] ?>
<?= $foo[$bar] ?>

<?php define('DEFINED_CONSTANT','SOME DEFINITION HERE'); ?>
<?= DEFINED_CONSTANT ?>
<?= UNDEFINED_CONSTANT ?>

<? if($foo_bar['test']) ?>
<?php $foo_bar['test']='bar'; ?>
Username: <?= $foo_bar['username'] ?>
Password: <?= $foo_bar['password'] ?>
<?= $foo_bar['test'] ?>
<?echo 'i told you so';?>


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






[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux