Solved

preg_replace help - ignore spaces and tabs

Posted on 2008-06-17
4
336 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
  • 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now