Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

preg_replace help - ignore spaces and tabs

Posted on 2008-06-17
4
Medium Priority
?
361 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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

704 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