What happens in php if you put the $ in a character class; e.g. [$] ? Peter West And he said to them, "Do you not yet understand?" On 21 Feb 2014, at 5:34 am, Daevid Vincent <daevid@xxxxxxxxxx> 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) > > 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 > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php