Solved

preg_replace help - ignore spaces and tabs

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

792 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