Solved

Refinement of REGEX in preg_replace stmt

Posted on 2008-06-24
6
236 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

734 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