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

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

 



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 ?>





--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/

--
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