Solved

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

Posted on 2008-06-17
10
229 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

813 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

16 Experts available now in Live!

Get 1:1 Help Now