Solved

Refinement of REGEX in preg_replace stmt

Posted on 2008-06-24
6
231 Views
Last Modified: 2010-04-21
This question is related to my previous question:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23491772.html

The following preg_replace regex works fine to parse out the comments for arrays like noted in the question above.

// Check and see if line has a comment for language element - assume comment is for last found language variable
if(preg_match('#\'(\w+)\'\s*=>\s*\'([^\']*)\'\s*[,]?\s*(//.*)$#i', trim($line), $match) && !empty($match[1])) {
   $this->_comments[$lastvar][$match[1]] = array('value' => $match[2], 'comment' => $match[3]);
}

What I've noticed now is that some arrays that I need to parse in the file are like the following:

$LANG_GF06 = array (
    1   => 'Statistics',      // Admin Navbar - 1st Tab - Statistics
    2   => 'Settings',        // Admin Navbar - 2nd Tab - Settings
    3   => 'Forums',          // Admin Navbar - 3rd Tab - Forums
    4   => 'Moderator',       // Admin Navbar - 4th Tab - Moderators
    5   => 'Convert',         // Admin Navbar - 5th Tab - Convert (convert stories to forum topics)
    6   => 'Messages',        // Admin Navbar - 6th Tab - Messages
    7   => 'IP Mgmt'          // Admin Navbar - 7th Tab - IP Mgmt (Banned users)
);

Can anyone suggest a tweak to that REGEX so it will handle arrays with a 'text' key as well as numeric keys?
0
Comment
Question by:Bl248
  • 4
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
margajet24 earned 500 total points
ID: 21861579
the "(\w+)" is for alphanumeric characters with one or more repititions.


if that didn't work for you, please try this :

\'?([A-Za-z0-9]+)\'?\s*=>\s*\'([^\']*)\'\s*[,]?\s*(//.*)$
^                         ^

added also "?" to make ' zero or one repitition
0
 

Author Comment

by:Bl248
ID: 21864872
The array that uses numeric keys does not have quotes around the key but it looks like the regex above is still expecting or searching on quote delimiters.
0
 
LVL 10

Expert Comment

by:margajet24
ID: 21865671
\'? - checks whether there is a 1  \' or none..
thus, 0 or 1 repitition..
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Bl248
ID: 21869404
My new preg_match stmt is:
if(preg_match('#\'\'?([A-Za-z0-9]+)\'?\'\s*=>\s*\'([^\']*)\'\s*[,]?\s*(//.*)$#i', trim($line), $match) && !empty($match[1])) {
    $this->_comments[$lastvar][$match[1]] = array('value' => $match[2], 'comment' => $match[3]);
}

It is still not picking up the integer array records unless I quote them.
Example:
$LANG_GF06 = array (
    1   => 'Statistics',      // Admin Navbar - 1st Tab - Statistics
    '2' => 'Settings',        // Admin Navbar - 2nd Tab - Settings
    3   => 'Forums',          // Admin Navbar - 3rd Tab - Forums
    4   => 'Moderator',       // Admin Navbar - 4th Tab - Moderators
    5   => 'Convert',         // Admin Navbar - 5th Tab - Convert (convert stories to forum topics)
    6   => 'Messages',        // Admin Navbar - 6th Tab - Messages
    7   => 'IP Mgmt'          // Admin Navbar - 7th Tab - IP Mgmt (Banned users)
);

Only record '2' is parsed.
0
 

Author Comment

by:Bl248
ID: 21869431
Sorry - this works which is what you meant:
if(preg_match('#\'?([A-Za-z0-9]+)\'?\s*=>\s*\'([^\']*)\'\s*[,]?\s*(//.*)$#i', trim($line), $match) && !empty($match[1])) {
    $this->_comments[$lastvar][$match[1]] = array('value' => $match[2], 'comment' => $match[3]);
}
0
 

Author Closing Comment

by:Bl248
ID: 31470414
Thanks for the help!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

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…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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 look for a specific file type in a local or remote server directory using PHP.

743 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

14 Experts available now in Live!

Get 1:1 Help Now