Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-17
10
Medium Priority
?
236 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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