Re: Anyone have a tool/script to convert <? to <?php (but not <?=)
| From: | Jim Lucas | Date: | Thu, 20 Feb 2014 22:05:03 +0000 |
| Subject: | Re: Anyone have a tool/script to convert <? to <?php (but not <?=) | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-322955@lists.php.net to get a copy of this message | ||
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)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 <?phpphp > 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_replacephp > 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 instancephp > 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
$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/