【问题标题】:Processing text in php用php处理文本
【发布时间】:2015-02-24 08:50:54
【问题描述】:

我想在遇到某些字符串时以这样的方式处理 php 中的文本。删除遇到该字符串的新行,包括该行之后的行。例如我有如下内容:

This is the sample content.

On 2015-02-19 00:35, notifications@test.com wrote:
> line 1
> line 2 etc

我只想要有意义的内容,在这种情况下

This is the sample content.

然后我想跳过所有内容。目前,我使用

preg_match("/\b, notifications@test.com\b/i",$task_comment_descption,$matches)

但是使用这个我得到,这是示例内容。于 2015-02-19 00:35

【问题讨论】:

  • 什么是“有意义的内容”——以清晰的方式给出示例输入和示例输出。
  • 这是我现在的实际输出。这是示例内容。在 2015-02-19 00:35,notifications@test.com 写道: > line 1 > line 2 etc.
  • 我只想拥有“这是示例内容”。仅作为最终输出。
  • 请重新标记此帖子,如果您想解决此帖子,请添加 Regex 标签。

标签: php regex email parsing preg-match


【解决方案1】:

我会这样做:

$array = array(
'This is the sample content.',
' ',
'On 2015-02-19 00:35, notifications@test.com wrote:',
'> line 1',
'> line 2 etc',
);
$res = array();
foreach ($array as $line) {
    if (preg_match('/\bnotifications@test\.com\b/i', $line)) {
        break;
    }
    $res[] = $line;
}
print_r($res);

输出:

Array
(
    [0] => This is the sample content.
    [1] =>  
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    相关资源
    最近更新 更多