Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查每一行是否符合某个字符的最有效方法是什么_Php_Parsing - Fatal编程技术网

Php 检查每一行是否符合某个字符的最有效方法是什么

Php 检查每一行是否符合某个字符的最有效方法是什么,php,parsing,Php,Parsing,如果我将一封电子邮件导入一个PHP脚本,并使用fgets遍历每一行,那么从from:到:和主题:行中过滤出值的最佳方法是什么 我想在:处分解每一行,然后检查是否($result[0]==='To')等等,但随后我分解每一行,而我应该只对从,到和主题 在Google/SO搜索中,我找到了substr(),但您需要指定要搜索的字符数,这对于我想要的每个值都是不同的。有一个页面可以解析邮件标题-它们共享它们正在使用的正则表达式,因此您可以简单地获取所需的部分: 为所需字段建议的正则表达式包括: Fr

如果我将一封电子邮件导入一个PHP脚本,并使用
fgets
遍历每一行,那么从
from:
到:
主题:
行中过滤出值的最佳方法是什么

我想在
处分解每一行,然后检查
是否($result[0]==='To')
等等,但随后我分解每一行,而我应该只对
主题


在Google/SO搜索中,我找到了
substr()
,但您需要指定要搜索的字符数,这对于我想要的每个值都是不同的。

有一个页面可以解析邮件标题-它们共享它们正在使用的正则表达式,因此您可以简单地获取所需的部分:

为所需字段建议的正则表达式包括:

From     |^from:(.*)|mi 
To       |^to:(.*)|mi 
Subject  |^subject:(.*)|mi 

有一个页面可以解析邮件标题-他们正在共享他们正在使用的正则表达式,因此您可以简单地获取您需要的部分:

为所需字段建议的正则表达式包括:

From     |^from:(.*)|mi 
To       |^to:(.*)|mi 
Subject  |^subject:(.*)|mi 

你可以这样做:

$finds = array('Subject:', 'From:', 'To:');
$founds = array();

$line = fgets(...); // whatever your logic is to get each line

foreach($finds as $key => $find){

    if (substr(trim($line), 0, strlen($find)) == $find){
        $founds[] = array($find, $line);
    }

} 

print_r($founds);  // you can explode (or whatever) the data as needed

这是未经测试的,我只是将其打印出来:)

您可以执行以下操作:

$finds = array('Subject:', 'From:', 'To:');
$founds = array();

$line = fgets(...); // whatever your logic is to get each line

foreach($finds as $key => $find){

    if (substr(trim($line), 0, strlen($find)) == $find){
        $founds[] = array($find, $line);
    }

} 

print_r($founds);  // you can explode (or whatever) the data as needed

这是未经测试的,我只是把它打出来:)

您可以使用正则表达式执行类似的操作,对于每个输入
$line

$matches = array();
$params = array();
if (preg_match('/^Subject: (.*)/', $line, $matches) == 1) {
    $params['subject'] = $matches[1];
} elseif (preg_match('/^From: (.*)/', $line, $matches) == 1) {
    $params['from'] = $matches[1];
} elseif (preg_match('/^To: (.*)/', $line, $matches) == 1) {
    $params['to'] = $matches[1];
}

对于每个输入
$line
,可以使用正则表达式执行如下操作:

$matches = array();
$params = array();
if (preg_match('/^Subject: (.*)/', $line, $matches) == 1) {
    $params['subject'] = $matches[1];
} elseif (preg_match('/^From: (.*)/', $line, $matches) == 1) {
    $params['from'] = $matches[1];
} elseif (preg_match('/^To: (.*)/', $line, $matches) == 1) {
    $params['to'] = $matches[1];
}

我认为使用正则表达式是最好的方式。通过使用正则表达式,您可以根据需要过滤掉任何内容that@Lion除了HTML。我认为使用正则表达式是最好的方式。通过使用正则表达式,您可以根据需要过滤掉任何内容that@Lion除了HTML。除了HTML以外的任何内容。@PaulCrovella添加了有问题的表达式。@PaulCrovella添加了有问题的表达式。