解析程序:我如何错误地加载我的对象?
在我看来,if (isStart($line)){}
和 if (isEnd($line))
块将事物放入了错误的范围。问题区域在“/***PROBLEM AREA */
”周围进行注释。
这是我的解析程序:
<?php
//**CLASS AND OBJECT */
class Entry
{
private $reason;
private $s_id;
public function __construct()
{
$this->reason = '';
$this->s_id = '';
}
//** GETTERS AND SETTERS */
public function SetReason($reason)
{
$this->reason = $reason;
}
public function GetReason()
{
return $this->reason;
}
public function SetS_id($s_id)
{
$this->s_id = $s_id;
}
public function GetS_id()
{
return $this->s_id;
}
}
//** EXTRACTION FUNCTION(S)
function extractReason($line)
{
$matches;
preg_match('/^Reason:\s+(.*)\s+$/', $line, $matches);
return $matches[1];
}
function extractS_id($line)
{
$matches;
preg_match('/^S_id:\s+(.*)\s+$/', $line, $matches);
return $matches[1];
}
//** LINE CONTAINST DESIRED EXTRACTION CHECK */
function isStart($line)
{
return preg_match('/^Start$/', $line);
}
function isReason($line)
{
return preg_match('/^Reason:\s+(.*)$/', $line);
}
function isS_id($line)
{
return preg_match('/^S_id:\s+(.*)$/', $line);
}
function isContent($line)
{
return preg_match('/.*$/', $line);
}
function isEnd($line)
{
return preg_match('/^End$/', $line);
}
//** DEFINITION */
$fName = 'obfile_extractsample.txt';
$fh = fopen($fName, 'r');
$line;
$entry;
$entrys = array();
//** PARSE OPERATION
if ($fh === FALSE)
die ('Failed to open file.');
//**START PROBLEM AREA */
while (($line = fGets($fh)) !== FALSE)
{
if (isStart($line)){
$entry = new Entry();
if (isReason($line)){
$entry->SetReason(extractReason($line));
}
if (isS_id($line)){
$entry->SetS_id(extractS_id($line));
}
if (isEnd($line)){
$entrys[] = $entry;
}
}
}
//***END PROBLEM AREA */
echo "<pre>";
print_r($entrys);
echo "</pre>";
fclose($fh);
?>
这是我的示例文件:
Start
Name: David Foster
Out Time: 4:36 p.m.
Back Time: 4:57 p.m.
Reason: Lunch
S_id: 0611125
End
Start
Name: Brenda Banks
Out Time: 5:53 a.m.
Back Time: 6:30 a.m.
Reason: Personal
S_id: 0611147
End
这是输出:
Array()
重要编辑
isStart
和 isEnd
函数中输入的正则表达式字符串是不正确的。行尾之前有一些看不见的符号。正确的正则表达式模式是:'/^Start.*$/'
和 '/^End.*$/'
In my opinion, the if (isStart($line)){}
and if (isEnd($line))
blocks put things into the wrong scope. The problem area is commented around "/***PROBLEM AREA */
".
This is my parsing program:
<?php
//**CLASS AND OBJECT */
class Entry
{
private $reason;
private $s_id;
public function __construct()
{
$this->reason = '';
$this->s_id = '';
}
//** GETTERS AND SETTERS */
public function SetReason($reason)
{
$this->reason = $reason;
}
public function GetReason()
{
return $this->reason;
}
public function SetS_id($s_id)
{
$this->s_id = $s_id;
}
public function GetS_id()
{
return $this->s_id;
}
}
//** EXTRACTION FUNCTION(S)
function extractReason($line)
{
$matches;
preg_match('/^Reason:\s+(.*)\s+$/', $line, $matches);
return $matches[1];
}
function extractS_id($line)
{
$matches;
preg_match('/^S_id:\s+(.*)\s+$/', $line, $matches);
return $matches[1];
}
//** LINE CONTAINST DESIRED EXTRACTION CHECK */
function isStart($line)
{
return preg_match('/^Start$/', $line);
}
function isReason($line)
{
return preg_match('/^Reason:\s+(.*)$/', $line);
}
function isS_id($line)
{
return preg_match('/^S_id:\s+(.*)$/', $line);
}
function isContent($line)
{
return preg_match('/.*$/', $line);
}
function isEnd($line)
{
return preg_match('/^End$/', $line);
}
//** DEFINITION */
$fName = 'obfile_extractsample.txt';
$fh = fopen($fName, 'r');
$line;
$entry;
$entrys = array();
//** PARSE OPERATION
if ($fh === FALSE)
die ('Failed to open file.');
//**START PROBLEM AREA */
while (($line = fGets($fh)) !== FALSE)
{
if (isStart($line)){
$entry = new Entry();
if (isReason($line)){
$entry->SetReason(extractReason($line));
}
if (isS_id($line)){
$entry->SetS_id(extractS_id($line));
}
if (isEnd($line)){
$entrys[] = $entry;
}
}
}
//***END PROBLEM AREA */
echo "<pre>";
print_r($entrys);
echo "</pre>";
fclose($fh);
?>
This is my example file:
Start
Name: David Foster
Out Time: 4:36 p.m.
Back Time: 4:57 p.m.
Reason: Lunch
S_id: 0611125
End
Start
Name: Brenda Banks
Out Time: 5:53 a.m.
Back Time: 6:30 a.m.
Reason: Personal
S_id: 0611147
End
This is the output:
Array()
IMPORTANT EDIT
The regex string input in the isStart
and isEnd
functions was incorrect. There were some invisible symbols before the end of the line. The correct regex patterns are: '/^Start.*$/'
and '/^End.*$/'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个 bit:
总是会创建一个新的
Entry
并将其添加到数组中,但它唯一可以检测到的字段是Reason: ...
和Style: 。 ..
。因此,大多数行都会产生空的Entry
。This bit:
will always create a new
Entry
and add it to your array, but the only fields it can detect areReason: ...
andStyle: ...
. So most of the lines result in emptyEntry
s.