Link to home
Start Free TrialLog in
Avatar of Bl248
Bl248

asked on

preg_replace help and help with regex to handle escape characters

This question is related to a previous question but requires a refinement that I need help with.

https://www.experts-exchange.com/questions/23491772/preg-replace-help-ignore-spaces-and-tabs.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!
Avatar of Roonaan
Roonaan
Flag of Netherlands image

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

Avatar of Bl248
Bl248

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bl248

ASKER

Thanks very much for your help!