SoberDude Posted June 1, 2009 Share Posted June 1, 2009 I have a *.txt file that looks like: <?php $Hello="moo"; $BLAH_Hi="cookies"; echo "TEST"; ?> Okay, it doesn't look like that, but it's has the same concept (uppercase letters in variable names). Also, don't be confused: the above is a txt file, not a .php file. I need a way to use preg_replace/preg_replace_callback (whatever is best) in order to make all variable names only be consistent of lowercase letters, so the above would be: <?php $hello="moo"; $blah_hi="cookies"; echo "TEST"; ?> Is there a way to do this? Note: I only need the code to replace the uppercase letters in the variables. I don't need the code to write to a file, open it, or anything else. Just pretend the above was a string, and not a file. Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/ Share on other sites More sharing options...
MadTechie Posted June 1, 2009 Share Posted June 1, 2009 Something like this should work, <?php $text = '<?php $Hello="moo"; $BLAH_Hi="cookies"; echo "TEST"; ?>'; $text = preg_replace_callback('/(\$.*?;)/s',"toLower",$text); echo $text; function toLower($matches) { return strtolower($matches[1]); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846496 Share on other sites More sharing options...
.josh Posted June 1, 2009 Share Posted June 1, 2009 mm..think i'd rather do this: $text = preg_replace_callback('/(\$[^=]*=)/s',"toLower",$text); madtechie's would make make the values lowercase also.. Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846504 Share on other sites More sharing options...
MadTechie Posted June 1, 2009 Share Posted June 1, 2009 Ah Very true, I should of read it again, I thought he wanted the values lower! Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846513 Share on other sites More sharing options...
SoberDude Posted June 1, 2009 Author Share Posted June 1, 2009 Thanks for the help. It almost works, it's just that, sometimes it goes past just the variable, like: SELECT HIGH_PRIORITY * FROM users WHERE u_userid='$vp_userid' limit 1 "limit 1" should be "LIMIT 1". Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846549 Share on other sites More sharing options...
MadTechie Posted June 1, 2009 Share Posted June 1, 2009 Your example's above didn't show that, however try this <?php $text = '<?php $Hello="moo"; $BLAH_Hi="cookies"; echo "TEST"; $Query = "SELECT HIGH_PRIORITY * FROM users WHERE u_userid=\'$vp_userid\' limit 1" ?>'; $text = preg_replace_callback('/(\$.*?)[=\'"}{]/s',"toLower",$text); echo $text; function toLower($matches) { return strtolower($matches[1]); } ?> EDIT: Add }{ EDIT: #2 Read it this time! Why should limit be LIMIT ? that has nothing to do with the first post! in addition I'm not writing an whole clean up parse! Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846551 Share on other sites More sharing options...
SoberDude Posted June 1, 2009 Author Share Posted June 1, 2009 Huh? o.o I meant like, in some of my files, it has: SELECT * FROM table WHERE t_row='$Variable' LIMIT 1 and when I use the preg_replace you guys suggested, it changes it to: SELECT * FROM table WHERE t_row='$variable' limit 1 The $variable part is good, but I don't want it to change to "limit 1": I want it to stay as "LIMIT 1". XD Thanks Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846558 Share on other sites More sharing options...
MadTechie Posted June 1, 2009 Share Posted June 1, 2009 try my last script. should fix it, it won't change limit to LIMIT, but at the same time it won't affect LIMIT at all! Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846559 Share on other sites More sharing options...
SoberDude Posted June 1, 2009 Author Share Posted June 1, 2009 Well, it doesn't change LIMIT, but now is changes it to: SELECT * FROM table WHERE t_row='$variable LIMIT 1 (removes the ' after $variable). It even changes: <script type='text/javascript'> to <script type'text/javascript'> o.o Sorry to be a nuisance. Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846565 Share on other sites More sharing options...
MadTechie Posted June 1, 2009 Share Posted June 1, 2009 oops '/(\$.*?[=\'"}{])/s' should be '/(\$.*?)[=\'"}{]/s' Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846567 Share on other sites More sharing options...
SoberDude Posted June 1, 2009 Author Share Posted June 1, 2009 Um, now, it's destroying quotes, equal signs, and brackets. o.o Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846579 Share on other sites More sharing options...
MadTechie Posted June 1, 2009 Share Posted June 1, 2009 Heres the full code i am using $text = '<?php $Hello = "moo"; $BLAH_Hi="cookies"; echo "TEST"; $Query = "SELECT HIGH_PRIORITY * FROM users WHERE u_userid=\'$VP_userid\' limit 1" ?>'; <?php function toLower($matches) { return strtolower($matches[1]); } echo preg_replace_callback('/(\$.*?[=\'"}{])/s',"toLower",$text); ?> heres my output <?php $hello = "moo"; $blah_hi="cookies"; echo "TEST"; $query = "SELECT HIGH_PRIORITY * FROM users WHERE u_userid='$vp_userid' limit 1" ?> Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846588 Share on other sites More sharing options...
SoberDude Posted June 1, 2009 Author Share Posted June 1, 2009 Thank you so much for your help! It turns out I was accidentally using '/(\$.*?)[=\'"}{]/s' instead of the new one. Just one more thing: is it possible to make it only change variables that aren't like "$_POST"? Like, if the variable name has a _ in front of it, it will not execute. For example, these would not be changed: $_POST, $_GET, $_SERVER, $_COOKIE, $_SESSION, etc. These WOULD be changed though: $Hi, $BlaH, $ThIsIsmESseDUp, $Moo, $hI, etc. Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846598 Share on other sites More sharing options...
MadTechie Posted June 1, 2009 Share Posted June 1, 2009 '/(\$[^_].*?[=\'"}{])/s' would change <?php $Hello = "moo"; $BLAH_Hi="cookies"; echo "TEST"; $Query = "SELECT HIGH_PRIORITY * FROM users WHERE u_userid='$vp_userid' limit 1" $HelloPOst = $_POST['Test'].$Hello; <script type='text/javascript'> ?> to <?php $hello = "moo"; $blah_hi="cookies"; echo "TEST"; $query = "SELECT HIGH_PRIORITY * FROM users WHERE u_userid='$vp_userid' limit 1" $hellopost = $_POST['Test'].$hello; <script type='text/javascript'> ?> EDIT: corrected (didn't copy the results correctly) Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846605 Share on other sites More sharing options...
SoberDude Posted June 1, 2009 Author Share Posted June 1, 2009 Um, now it's messing around with things that have _ in it (other than variables), like ENT_NOQUOTES in htmlentities. o.o Not a big problem, I found a way around that (more or less o.o). Anyway, now, for echos, if you have: echo "HI, $Moo COWS!!"; it changes to echo "HI, $moo cows!!"; Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846619 Share on other sites More sharing options...
MadTechie Posted June 1, 2009 Share Posted June 1, 2009 okay, this should work '/(\$[^_].*?[\s=\'"}{])/s' Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846634 Share on other sites More sharing options...
SoberDude Posted June 1, 2009 Author Share Posted June 1, 2009 It still seems to do it. o.o Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846776 Share on other sites More sharing options...
MadTechie Posted June 1, 2009 Share Posted June 1, 2009 Solved ? Click Topic Solved if it is Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-846866 Share on other sites More sharing options...
SoberDude Posted June 1, 2009 Author Share Posted June 1, 2009 I was at school. =P I tested it quickly before leaving, and it screwed up, so I waited to get home, but it was just because I did it wrong. So far, it seems to work fine. Thank you so much for all your help! Quote Link to comment https://forums.phpfreaks.com/topic/160419-solved-lowercase-with-preg_replace/#findComment-847152 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.