Solved

Parsing file - locate and return matching comment for line (regex possible)

Posted on 2008-06-17
10
233 Views
Last Modified: 2010-08-05
I have a PHP file that I want to read and parse to capture into a variable the matching comment (should it exist) for a matching array element.

So if this define is in the file I am parsing and I search on the array key 'most_viewed' - I would like to determine if a comment exits on that line and if so what the comment is.

$fp = fopen($file);
$contents = fread($fp);
 .. now search for line matching array key and return comment if it exists.



$LANG_MGE01 = array (
    'featured_series'                   => 'Featured Series',
    'featured_episodes'                 => 'Featured Episodes',
    'featured'                          => 'Featured',
    'most_viewed'                       => 'xMost Viewed',            //  This is a test comment - most viewed
}
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
  • 7
  • 3
10 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 21804479
$lines = file($file);
foreach($lines as $index => $line) {

  if(preg_match('#\'most_viewed\'\s*=>\s*\'[^\']*\'\s*[,]?\s*(//.*)$#i", trim($line), $match) && !empty($match[1])) {
     $most_viewed_comment = $match[1];
     echo $most_viewed;
  }
}
0
 

Author Comment

by:Bl248
ID: 21804652
Thanks again - that worked well.

Just had a few typos - missing quote and last echo was the wrong variable but they were easy to fix. That hard part for me was the regex - so thanks a bunch.

$lines = file($langfile);
$token = 'most_viewed';
foreach($lines as $index => $line) {
  if(preg_match('#\''.preg_quote($token).'\'\s*=>\s*\'[^\']*\'\s*[,]?\s*(//.*)$#i', trim($line), $match) && !empty($match[1])) {
     $most_viewed_comment = $match[1];
     echo $most_viewed_comment;
  }
}
0
 

Author Closing Comment

by:Bl248
ID: 31468009
Thanks again for the very very quick solution!
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: 21806337
Have a related quesiton.

How would I get the array key for that matching line using preg_match?

I could use php explode or sub_str after getting the character positions but a preg_replace would appear to be cleaner.

    'most_recent'                       => 'Most Recent',            //  This is a test comment - most recent
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21806375
the array key would be $token, wouldn't it be?
0
 

Author Comment

by:Bl248
ID: 21806441
True, in this case and should have added that the new scenario that I am testing is:

1) Scan the file looking for config elements with comments
2) Extract out the element key, value and comment.

I am looking at creating a small admin tool to allow users to maintain the language defines online.  Only defines with a comment and the comment is used to explain the variables use.

So that would introduce a change to your code extract, where now we search each line for a comment and then break it out into 3 parts.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21806481
$config = array();

$lines = file($langfile);
foreach($lines as $index => $line) {
  if(preg_match('#\'(\w+)\'\s*=>\s*\'([^\']*)\'\s*[,]?\s*(//.*)$#i', trim($line), $match) && !empty($match[1])) {
     $config[$match[1]] = array('value' => $match[2], 'comment' => $match[3]);
  }
}

print_r($config);
0
 

Author Comment

by:Bl248
ID: 21806524
That works very nicely -- going to work with that now :)
Thanks!
0
 

Author Comment

by:Bl248
ID: 21813101
One more tweak and please let me know if I should add more points to this question :)

if the line does not have a comment - so an else part of this:
 Return into #match parts 1 and 2 anyways. I will be driving an admin form and allowing the user to update this file.

Will there be an issue if the comment on the line has a quote as in
 // this ain't such a good comment

Thanks!
0
 

Author Comment

by:Bl248
ID: 21813920
Never mind -- I have found an alternative way to do what I wanted :)
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL limit and not so limited 13 62
[form-control] Retain values after a POST action 21 76
php output utf-8 problem 6 52
htaccess add www, redirect to https, keep query string 17 60
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
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…
The viewer will learn how to count occurrences of each item in an array.

710 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