Solved

preg_replace help and help with regex to handle escape characters

Posted on 2008-06-18
4
421 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
[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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP 5.6 and 7.x 4 38
Google Dork query 7 62
Converting PHP triple des decrypt to c# 9 65
Displaying a google org chart from php/mysql code 27 20
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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 count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

756 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