Link to home
Start Free TrialLog in
Avatar of Bl248
Bl248

asked on

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

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
}
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bl248
Bl248

ASKER

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;
  }
}
Avatar of Bl248

ASKER

Thanks again for the very very quick solution!
Avatar of Bl248

ASKER

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
the array key would be $token, wouldn't it be?
Avatar of Bl248

ASKER

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.
$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);
Avatar of Bl248

ASKER

That works very nicely -- going to work with that now :)
Thanks!
Avatar of Bl248

ASKER

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!
Avatar of Bl248

ASKER

Never mind -- I have found an alternative way to do what I wanted :)