Solved

preg_replace help and help with regex to handle escape characters

Posted on 2008-06-18
4
408 Views
Last Modified: 2010-05-18
This question is related to a previous question but requires a refinement that I need help with.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23491772.html

Problem: I am reading in a language file and updating it online and need to preserve the file formatting.
Mostly working except for situation with quotes in the replacement text.

I need a preg_replace that will update the one line searching on an array key ($token) + (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'.

Example array in file:

$LANG_MGE07 = array (
    'search_message'                    => 'Searched for the phrase \'%s\'. Found %s items (%s seconds).',
    'member_profile_pagetitle'                => 'Member profile for:',
    'watch_default_pagetitle'                  => 'Watch Content - Home page',
    'watch_series_pagetitle'            => 'Watch Content - Series View',
    'watch_episodes_pagetitle'          => 'Watch Content - Episodes View',
    'no_search_results'                 => 'No results found matching your search request'
);

A more extreme example:
    'register_failed_message'       => '<span style="color:red">The username or email you\\\'ve provided are already in use. Please try again...</span>',  

The existing function in my class that works fine (thanks Roonaan) except in the condition where the replacement text has quotes - escaped.

    function updateToken($token, $newvalue)
    {
        $this->_content = preg_replace('#(\''.preg_quote($token).'\'\s+=>\s+)\'[^\']+\'#i', '$1\''.$newvalue.'\'', $this->_content);
    }

Can this be resolved ?

Thanks!
0
Comment
Question by:Bl248
  • 2
  • 2
4 Comments
 
LVL 49

Expert Comment

by:Roonaan
Comment Utility
An alternative would be to use token-get-all. This will give you an array identifing all data as php tokens:

http://us2.php.net/manual/en/function.token-get-all.php

0
 

Author Comment

by:Bl248
Comment Utility
It's not that I need to parse this file to set defines that I use in my application. I need to effectively re-write this file and want to preserve any formatting and extra code, comments that are not part of the config variables that I want to update.

Once I know the $LANG_xxxxx name, I can change the value just fine in memory and code. It's how now to I update the value in the php file.

I felt that doing a fread of the file and using preg_replace on the matching token and then re-writting the file was the best solution.

I can read the file line by line on each replacement/update and find/replace the complete line but that would appear to me to be a lot of code/looping on 100's of replacements.

Is there not a regex option to add an match on quote but igore a \'

Thanks again for your help!
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
Comment Utility
Easier said than done. I ran some tests with the below, which seems to work. I might however overlooked some situations which are specific to your application:

      $this->_content = preg_replace("#('$token'\s+=>\s+)'([^\\\']|\\\.)*'#i", '$1\''.$newvalue.'\'', $this->_content);
0
 

Author Comment

by:Bl248
Comment Utility
Thanks very much for your help!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
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…

763 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

9 Experts available now in Live!

Get 1:1 Help Now