Reputation: 408
I'm working on my php to fetch the email data from the email body. I have got a problem with removing the <br />
tags before the strings This message was created
.
Here is the full email body:
<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 />
[email protected]<br />
retry timeout exceeded
I have tried this:
$top_message = str_replace('<br /> <br /> This message', 'This message', $top_message);
And I have also tried this:
$top_message = str_replace('<br /> <br />', '', $top_message);
It will not remove the <br />
tags before the strings and nothing happens.
Here is the full code:
$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
What I am trying to achieve is when I am fetching the email data from the email body, I want to use nl2br
to add the <br />
tags for each line and then I want to remove the two <br />
tags before the strings This message was created
.
I want to make it to show like this:
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 />
[email protected]<br />
retry timeout exceeded
Can you please show me an example how I could be able to remove the two <br />
tags before the strings?
Thank you.
Upvotes: 1
Views: 1048
Reputation: 42695
What I am trying to achieve is when I am fetching the email data from the email body, I want to use nl2br to add the
<br />
tags for each line and then I want to remove the two<br />
tags before the string "This message was created".
But why would you do that? Just don't add the <br/>
tags to begin with.
$top_message = nl2br(trim($top_message));
Upvotes: 0
Reputation: 23958
You can use ltrim() which will remove characters according to a character mask.
Meaning any <
and any b
and any r
and any /
and any >
until it finds a different character.
$message = ltrim($message, "<br/> ");
I added space also because it will be "optional" anyways.
Please not that this will not work if the answer to my comment is no, meaning the message does not always start with "this message..".
Because if the string is "<br> <br> break free..."
the the output will be "eak free..."
because the character mask will remove "br" in "break".
And it will also remove if the word starts with "rb".
Upvotes: 1
Reputation: 3238
It looks like you have a newline (\n) there between the <br>'s. Please try this:
$top_message = str_replace("/software\.<br \/>\n<br \/>/", 'software.', $top_message);
Upvotes: 0
Reputation: 3623
Use preg_replace
with the following regular expression: /^(<br\s*\/>\s*)*/
.
It will remove all <br/>
tags at the beginning of your message.
$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 />
[email protected]<br />
retry timeout exceeded";
print_r(preg_replace('/^(<br\s*\/>\s*)*/', '', $str));
Output:
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 />
[email protected]<br />
retry timeout exceeded
Upvotes: 1