Solved

Refinement of REGEX in preg_replace stmt

Posted on 2008-06-24
6
235 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
[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
  • 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
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!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ajax and PHP 4 47
How Close unsubmited attempts 10 46
htaccess block all countries but the US 4 41
How to remove  "0":  in a json string 4 17
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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.
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 …

726 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