开发者

Reading from a file

I have file with contents:

开发者_高级运维
Tom TOM is a good student with excellent marks. This profile can be viewed online ar www.x.xom/tom/marks. He is one of the oustanding student
Tom1 TOM is a good student with excellent marks. This profile can be viewed online ar www.x.xom/tom/marks. He is one of the oustanding student
Tom2 TOM is a good student with excellent marks. This profile can be viewed online ar www.x.xom/tom/marks. He is one of the oustanding student

I have many lines like this. Each line is having a name followed by a string untill end of the line.

I want to have a php script that will read first words keeps in a temp variable. similarly I want all the text upto the end of the line to be going to the second variable.

I have used these

$myFile = "profiles.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 5);
fclose($fh);
echo $theData;

but the issue with that is I am able to read the first 5 characters. I want to read first time only one word. second one is all string to be going to a variable.


Something like this?

$myFile = "profiles.txt";
$fh = fopen($myFile, 'r');
$anArray = array();
while (($line = fgets($fg)) !== false) {
  list($name, $restOfLine) = explode(' ', $line, 2);
  // Do something with $name and $restOfLine (e.g. put them in array)
  $anArray[] = array('name' => $name, 'line' => $restofLine);
}
fclose($fh);
var_dump($anArray);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜