RE: [PHP] Anyone have a tool/script to convert <? to <?php (but not <?=)
| From: | Daevid Vincent | Date: | Thu, 20 Feb 2014 19:34:36 +0000 |
| Subject: | RE: [PHP] Anyone have a tool/script to convert <? to <?php (but not <?=) | ||
| References: | 1 2 3 4 5 6 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-322954@lists.php.net to get a copy of this message | ||
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)
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