preg_replace

  • camperjohn
  • Guru
  • Guru
  • User avatar
  • Joined: Nov 28, 2004
  • Posts: 1127
  • Loc: San Diego
  • Status: Offline

Post March 3rd, 2010, 3:08 pm

I hate regular expressions....

How do I do this: remove a line of code, from a configuration file, using a match


config.php
Code: [ Select ]
<?php
  $bd_server['test'] = "blahblah0";
  $bd_server['test2'] = "blahblah1"; <- this line is to be removed
//  $bd_server['test2'] = "blahblah2"; <- this line should stay because of comment
[space][space][space](etc)//  $bd_server['test2'] = "blahblah3"; <- this line should also stay, but is removed because comment is indented and not at {0}
?>
  1. <?php
  2.   $bd_server['test'] = "blahblah0";
  3.   $bd_server['test2'] = "blahblah1"; <- this line is to be removed
  4. //  $bd_server['test2'] = "blahblah2"; <- this line should stay because of comment
  5. [space][space][space](etc)//  $bd_server['test2'] = "blahblah3"; <- this line should also stay, but is removed because comment is indented and not at {0}
  6. ?>


test.php
Code: [ Select ]
$file = file_get_contents("config.php");

echo htmlentities(preg_replace('!^[^/].*bd_server\[\'test2\'\].*$!mi','',$file));
  1. $file = file_get_contents("config.php");
  2. echo htmlentities(preg_replace('!^[^/].*bd_server\[\'test2\'\].*$!mi','',$file));


desired result:
Code: [ Select ]
<?php
  $bd_server['test'] = "blahblah0";
//  $bd_server['test2'] = "blahblah2"; <- this line should stay because of comment
[space][space][space](etc)//  $bd_server['test2'] = "blahblah3"; <- this line should also stay, but is removed because comment is indented and not at {0}
?>
  1. <?php
  2.   $bd_server['test'] = "blahblah0";
  3. //  $bd_server['test2'] = "blahblah2"; <- this line should stay because of comment
  4. [space][space][space](etc)//  $bd_server['test2'] = "blahblah3"; <- this line should also stay, but is removed because comment is indented and not at {0}
  5. ?>


actual result:
Code: [ Select ]
<?php
  $bd_server['test'] = "blahblah0";
//  $bd_server['test2'] = "blahblah2"; <- this line should stay because of comment
?>
  1. <?php
  2.   $bd_server['test'] = "blahblah0";
  3. //  $bd_server['test2'] = "blahblah2"; <- this line should stay because of comment
  4. ?>


If I run test.php, it loads config.php and attempts to remove the line, blahblah 1 gets removed OK, but ALSO blahblah 3. I want 3 to stay because of the comment. blahblah 2 stays successfully.

Ideas?
Upload video and picture galleries at http://www.bodydot.com?post+upload+video+picture+gallery
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post March 3rd, 2010, 3:08 pm

  • camperjohn
  • Guru
  • Guru
  • User avatar
  • Joined: Nov 28, 2004
  • Posts: 1127
  • Loc: San Diego
  • Status: Offline

Post March 3rd, 2010, 3:13 pm

In other words, how do I test for "replace this, but not if there is a doubleslash before the match, even if there are tabs and spaces"
Upload video and picture galleries at http://www.bodydot.com?post+upload+video+picture+gallery
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post March 3rd, 2010, 9:39 pm

You're making sure that the first character isn't a forward-slash, but then you're using "anything zero or more times" after it which allows a forward slash to come in.

What are the chances your config files have # style comments ?
Just checking for slashes would miss those.

Code: [ Select ]
# $commented = '';


How about this

Code: [ Select ]
!^\s*\$bd_server\[\'test2\'\].*$!mi


It allows for whitespace at the beginning of the line, then only matches the variable.
Strong with this one, the sudo is.
  • camperjohn
  • Guru
  • Guru
  • User avatar
  • Joined: Nov 28, 2004
  • Posts: 1127
  • Loc: San Diego
  • Status: Offline

Post March 3rd, 2010, 11:58 pm

Well PHP doesnt support # as a commentor. I am actually searching in a PHP file and replacing.

So I do need to check for // anywhere before the pattern. I will not check for /* */ combos
Upload video and picture galleries at http://www.bodydot.com?post+upload+video+picture+gallery
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post March 4th, 2010, 9:00 am

Quote:
Well PHP doesnt support # as a commentor.


Since when ?
Strong with this one, the sudo is.
  • camperjohn
  • Guru
  • Guru
  • User avatar
  • Joined: Nov 28, 2004
  • Posts: 1127
  • Loc: San Diego
  • Status: Offline

Post March 4th, 2010, 10:50 am

Since camperjohn never uses it.
Upload video and picture galleries at http://www.bodydot.com?post+upload+video+picture+gallery
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post March 4th, 2010, 1:11 pm

Are you up and running with this now though, does the "whitespace zero or more times, followed by a variable" pattern work for you ?
Strong with this one, the sudo is.
  • camperjohn
  • Guru
  • Guru
  • User avatar
  • Joined: Nov 28, 2004
  • Posts: 1127
  • Loc: San Diego
  • Status: Offline

Post March 4th, 2010, 5:13 pm

joebert wrote:
Are you up and running with this now though, does the "whitespace zero or more times, followed by a variable" pattern work for you ?


Yes thank you good idea. That works.

It just means my patterns will correctly avoid:

// pattern
# pattern
(spacespacespace) // pattern

And will correctly catch

(spacespacespace) pattern

But will also avoid (even though it isn't a comment)

foobar pattern

Just means that anything before the pattern is considered a mismatch. This works fine for what I need.
Upload video and picture galleries at http://www.bodydot.com?post+upload+video+picture+gallery
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post March 4th, 2010, 8:43 pm

I figured it would considering you mentioned it's a config file. My guess is that anything other than space found before a variable declaration in a config file is probably going to be a syntax error in almost every situation anyways.
Strong with this one, the sudo is.

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 173 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.