Solved

preg_replace help and help with regex to handle escape characters

Posted on 2008-06-18
4
426 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

623 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