Jump to content

[SOLVED] Lowercase with preg_replace


SoberDude

Recommended Posts

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.

Link to comment
Share on other sites

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]);
}
?>

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

'/(\$[^_].*?[=\'"}{])/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)

Link to comment
Share on other sites

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!!";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.