preg_replace

  • camperjohn
  • Guru
  • Guru
  • User avatar
  • Posts: 1127
  • Loc: San Diego

Post 3+ Months Ago

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?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • camperjohn
  • Guru
  • Guru
  • User avatar
  • Posts: 1127
  • Loc: San Diego

Post 3+ Months Ago

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"
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

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.
  • camperjohn
  • Guru
  • Guru
  • User avatar
  • Posts: 1127
  • Loc: San Diego

Post 3+ Months Ago

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
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Quote:
Well PHP doesnt support # as a commentor.


Since when ?
  • camperjohn
  • Guru
  • Guru
  • User avatar
  • Posts: 1127
  • Loc: San Diego

Post 3+ Months Ago

Since camperjohn never uses it.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Are you up and running with this now though, does the "whitespace zero or more times, followed by a variable" pattern work for you ?
  • camperjohn
  • Guru
  • Guru
  • User avatar
  • Posts: 1127
  • Loc: San Diego

Post 3+ Months Ago

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.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

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.

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 99 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.