Solved

preg_replace help and help with regex to handle escape characters

Posted on 2008-06-18
4
419 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
ID: 21820180
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
ID: 21821484
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
ID: 21825256
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
ID: 21828018
Thanks very much for your help!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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 how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 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