Solved

preg_replace help - ignore spaces and tabs

Posted on 2008-06-17
4
358 Views
Last Modified: 2010-04-21
I want to parse a PHP config file and replace certain defines.

So if I have an array like the following and want to read the file and replace the define for 'most_viewed'. I could do a preg_replace on that line and re-write the file. I need a preg_replace that will update that one line searching on 'most_viewed' + (n number of spaces or tabs) + '==>' + (n number of spaces or tabs) and replace it with the same formatting except with my new 'string here'.

$LANG_MGE01 = array (
    'featured_series'                   => 'Featured Series',
    'featured_episodes'                 => 'Featured Episodes',
    'featured'                          => 'Featured',
    'most_viewed'                       => 'Most Viewed',
    'most_discussed'                    => 'Most Discussed',
    'most_recent'                       => 'Most Recent'
}

Can that be done with a preg_replace where the search string match is saved as an argument and then used in the replace so the line formatting is preserved.
0
Comment
Question by:Bl248
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 21803827
Try:

$token = 'most_viewed';
$new_value = 'Meest bekeken';

$new_config = preg_replace('#(\''.preg_quote($token).'\'\s+=>\s+)\'[^\']+\'#i', '$1\''.$new_value.'\'', $config);
0
 

Author Comment

by:Bl248
ID: 21803858
Howly crap that was fast -- I will give it a try :)
0
 

Author Closing Comment

by:Bl248
ID: 31467988
That is working quite nicely - thanks!
0
 

Author Comment

by:Bl248
ID: 21812553
While testing, I came across an issue with the above solution and wanted to see if I could request a fix (please).

If the contents that I want to replace have an escaped quote then the replace on text to replace between quotes fails - or inserts the new text in the middle of the old text is a better descriptiopn of the issue.

Example:
$LANG_MGE01 = array (
    'featured_series'                   => 'Featured Series',             // Testing
    'featured_episodes'                 => 'Featured Episodes',
    'featured'                          => 'Ain\'t this a featured item',
    'most_recent_featured'              => 'Most Recent',   // Test comment - most recent
    'most_viewed'                       => 'Most Viewed'
}

Doing a replace on token 'featured' will have an issue.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question