我正在使用php从电子邮件正文中获取电子邮件数据。我在删除字符串<br />
之前的This message was created
标签时遇到了问题。
这是完整的电子邮件正文:
<br />
<br />
This message was created automatically by mail delivery software.<br />
<br />
A message that you sent could not be delivered to one or more of its<br />
recipients. This is a permanent error. The following address(es) failed:
<br />
<br />
fvsafsafsaf@shitmail.com<br />
retry timeout exceeded
我已经尝试过了:
$top_message = str_replace('<br /> <br /> This message', 'This message', $top_message);
我也尝试过这个:
$top_message = str_replace('<br /> <br />', '', $top_message);
在字符串之前不会删除<br />
标签,什么也没发生。
这是完整的代码:
$body = imap_body($mailbox, $email_number, 2);
$email_body = utf8_decode(imap_utf8($body));
$top_message = getBetween($email_body, 'charset=us-ascii', 'exceeded') . 'exceeded';
$top_message = nl2br($top_message);
$top_message = str_replace('<br /> <br /> This message', 'This message', $top_message);
echo $top_message
我想要实现的是,当我从电子邮件正文中获取电子邮件数据时,我想使用nl2br
为每行添加<br />
标签,然后删除字符串<br />
之前的两个This message was created
标签。
我想让它像这样显示:
This message was created automatically by mail delivery software.<br />
<br />
A message that you sent could not be delivered to one or more of its<br />
recipients. This is a permanent error. The following address(es) failed:
<br />
<br />
name@example.com<br />
retry timeout exceeded
能否请您举一个示例,说明如何删除字符串前的两个<br />
标签?
谢谢。
答案 0 :(得分:1)
将preg_replace
与以下正则表达式结合使用:/^(<br\s*\/>\s*)*/
。
它将删除邮件开头的所有<br/>
标签。
$str = "<br />
<br />
This message was created automatically by mail delivery software.<br />
<br />
A message that you sent could not be delivered to one or more of its<br />
recipients. This is a permanent error. The following address(es) failed:
<br />
<br />
fvsafsafsaf@shitmail.com<br />
retry timeout exceeded";
print_r(preg_replace('/^(<br\s*\/>\s*)*/', '', $str));
输出:
This message was created automatically by mail delivery software.<br />
<br />
A message that you sent could not be delivered to one or more of its<br />
recipients. This is a permanent error. The following address(es) failed:
<br />
<br />
fvsafsafsaf@shitmail.com<br />
retry timeout exceeded
答案 1 :(得分:1)
您可以使用ltrim()来根据字符掩码删除字符。
表示任何<
和任何b
和任何r
和任何/
和任何>
,直到找到不同的字符为止。
$message = ltrim($message, "<br/> ");
我还添加了空格,因为无论如何它都是“可选的”。
请不要说,如果对我的评论的回答为“否”,这将不起作用,这意味着该消息并非总是以“此消息..”开头。
因为如果字符串是"<br> <br> break free..."
,则输出将是"eak free..."
,因为字符掩码将删除“ break”中的“ br”。
如果单词以“ rb”开头,也会删除。
答案 2 :(得分:0)
您似乎在
之间有一个换行符(\ n)。请尝试以下方法:
$top_message = str_replace("/software\.<br \/>\n<br \/>/", 'software.', $top_message);
答案 3 :(得分:0)
我要实现的是,当我从电子邮件正文中获取电子邮件数据时,我想使用nl2br为每行添加
<br />
标签,然后我要删除两个{{1 }}标记在字符串“此消息已创建”之前。
但是您为什么要这样做?只是不要添加<br />
标记。
<br/>