gudfry Posted July 23, 2008 Share Posted July 23, 2008 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 Link to comment https://forums.phpfreaks.com/topic/116112-solved-how-to-read-the-line-of-file-and-get-only-the-keyword/ Share on other sites More sharing options...
unkwntech Posted July 23, 2008 Share Posted July 23, 2008 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. Link to comment https://forums.phpfreaks.com/topic/116112-solved-how-to-read-the-line-of-file-and-get-only-the-keyword/#findComment-597088 Share on other sites More sharing options...
unkwntech Posted July 23, 2008 Share Posted July 23, 2008 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. Link to comment https://forums.phpfreaks.com/topic/116112-solved-how-to-read-the-line-of-file-and-get-only-the-keyword/#findComment-597092 Share on other sites More sharing options...
gudfry Posted July 23, 2008 Author Share Posted July 23, 2008 hi all; I ahve now solve my problem and thank with your help, I got to next assignment now. Link to comment https://forums.phpfreaks.com/topic/116112-solved-how-to-read-the-line-of-file-and-get-only-the-keyword/#findComment-597126 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.