0

Please tell me an easy and effective way for reading or getting information (whatever it is?) form the comment in php files, Whereas I know wordpress use this features.

Like I have a folder called theme and there are three files with this comment:

In first file

/* Template Name: page */

In second file

/* Template Name: category */ 

In third file

/* Template Name: Tags */

Then how whold I get page,category,tags in an array, Please give a solution

Thanks for your time

5
  • 2
    Less resource consuming than what? (ie, what are you doing that you want it faster and less consuming than?) Commented Apr 5, 2013 at 23:38
  • Reflection can get at this. Commented Apr 5, 2013 at 23:39
  • have you tried parse php files code source ? Commented Apr 5, 2013 at 23:39
  • @AbuRomaïssae No, Please give me an example Commented Apr 5, 2013 at 23:41
  • @user1983017, check my answer bellow Commented Apr 5, 2013 at 23:47

2 Answers 2

2

To pare the php files you can do as:

$str = file_get_contents("/path/to/file1");

than you complete php file code is there, and you will need to use some regex to get the comments content


example:

lets assume the $str have your php source code, the regex might be something like:

$str = "/* Template Name: page */\n bla bla blah";

preg_match( "#/\* Template Name: (\w*) \*/#", $str, $m);

var_dump( $m );

previous code output:

array(2) {
  [0]=>
  string(25) "/* Template Name: page */"
  [1]=>
  string(4) "page"
}

what you need is stored in $m[1]

Sign up to request clarification or add additional context in comments.

Comments

1
$source = file_get_contents($filename);
$tokens = token_get_all($source);
foreach($tokens as $token){
    if( ($token[0] == T_COMMENT || $token[0] == T_DOC_COMMENT)
       && preg_match('/Template Name: (.*)/',$token[1],$match){
       echo "Template name is ".$match[1];
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.