Solved

Refinement of REGEX in preg_replace stmt

Posted on 2008-06-24
6
233 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 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