Solved

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

Posted on 2008-06-17
10
224 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
Comment Utility
$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
Comment Utility
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
Comment Utility
Thanks again for the very very quick solution!
0
 

Author Comment

by:Bl248
Comment Utility
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
Comment Utility
the array key would be $token, wouldn't it be?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:Bl248
Comment Utility
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
Comment Utility
$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
Comment Utility
That works very nicely -- going to work with that now :)
Thanks!
0
 

Author Comment

by:Bl248
Comment Utility
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
Comment Utility
Never mind -- I have found an alternative way to do what I wanted :)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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 …

771 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

9 Experts available now in Live!

Get 1:1 Help Now