我有这个文本文件
acmst501:57:Runningacmst506:201:Runningacmst506:203:Runningacmst506:209:Runningacmst506:213:Failed
我想用分隔符分割。我有这个代码工作。
<?php
function parseFile($filename, $delimiter) {
global $splitcontents;
//$fd = fopen ($filename, "r");
//$contents = fread ($fd,filesize ($filename));
//fclose ($fd);
$contents = file_get_contents($filename);
$splitcontents = explode($delimiter, $contents);
return $splitcontents;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Read file and split with delimiter</title>
</head>
<body>
<?php
parseFile("c:\emap\asrvr505-cslot_state.txt",":");
foreach ( $splitcontents as $value ) {
echo "<b>$value<br>";
}
?>
</body>
</html>
我得到的输出是这样的:
acmst501
57
Running acmst506
201
Running acmst506
203
Running acmst506
209
Running acmst506
213
Failed
但是,我希望它是这样的:
acmst501
57
Running
acmst506
201
Running
...
所以基本上,我想将运行/失败部分与 acmst 字段分开。
我如何将这些分开?另外,我的函数是否正确,所以我可以将它与同一个 PHP 文件中的不同文件一起使用?谢谢!