Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

preg_replace help - ignore spaces and tabs

Posted on 2008-06-17
4
Medium Priority
?
363 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 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

885 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