Jump to content

[SOLVED] how to read the line of file and get only the keyword


gudfry

Recommended Posts

hi all

 

        I have a prblem onhow can i get the kewword of the line of file. I have a two file which is my function and the file I want to test.

 

        assuming a I have this code of file to test.

 

     

      test.php

      <?php

 

          /* $Author : Chad */

 

        ?>

         

       

    this is my code to get the file.

      $lines = file('test.php');

    foreach ($lines as $linenum => $line) {

    //echo "Lines #<b>{$linenum}</b> : " . htmlspecialchars($line) . "<br/>\n";

 

$input='/* $Author : Chad */';

$output=processLine($input);

if (gettype($output)=="array")

echo '<p>Title:'.($output[0]).' Value: '.$output[1].'</p>';

else

echo '<p>Not a string according to definition</p>';

}

  ?>

 

I want to get the this /* $Author : Chad */  and out this as a Author : Autbor Chad. how would i do this??? pls i need yuor help

do this

<?php
$file = "/path/to/file.ext";
$handle = fopen($file, "r+");
$data = fread($handle, filesize($file));
fclose($handle);
$data_array = explode("\n", $data);
$new_data = "";
foreach ($data_array as $value)
{
if (strstr($value, '$Author'))
 {
 	$data = split('/ : /', preg_replace('%(?:\/\* | \*| )%', '', $value));
 	//$data should be an array with $data['0'] == '$author' and $data['1'] == 'Chad'
 }
}
?>

Thanks a ton to jonsjava for this one.

If you wanted to take this further you would replace

if (strstr($value, '$Author'))

with

if(preg_match('%\/\*.*\*\/%', $value))

This will get more then just

/* $Author : Chad */

like for example mabey

/* $Copyright : GNUGPL */

And anything else that follows this same format.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.