删除字符串前的 <br /> 标签

我正在使用我的 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 />在字符串之前删除两个标签吗?


谢谢你。


POPMUISE
浏览 289回答 3
3回答

阿晨1998

使用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:&nbsp;<br /><br />&nbsp; &nbsp;fvsafsafsaf@shitmail.com<br />&nbsp; &nbsp; &nbsp; 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:&nbsp;<br /><br />&nbsp; &nbsp;fvsafsafsaf@shitmail.com<br />&nbsp; &nbsp; &nbsp; retry timeout exceeded

一只名叫tom的猫

您可以使用 ltrim() 它将根据字符掩码删除字符。意味着 any<和 anyb以及 anyr和/any>直到它找到不同的字符。$message&nbsp;=&nbsp;ltrim($message,&nbsp;"<br/>&nbsp;");我添加了空间也是因为它无论如何都是“可选的”。请注意,如果我的评论的答案是否定的,这将不起作用,这意味着该消息并不总是以“此消息..”开头。因为如果字符串是"<br> <br> break free..."输出将是"eak free..."因为字符掩码将删除“break”中的“br”。如果单词以“rb”开头,它也会删除。

MMTTMM

看起来您在 <br> 之间有一个换行符 (\n)。请试试这个:$top_message&nbsp;=&nbsp;str_replace("/software\.<br&nbsp;\/>\n<br&nbsp;\/>/",&nbsp;'software.',&nbsp;$top_message);
打开App,查看更多内容
随时随地看视频慕课网APP