使用域名和电子邮件的正则表达式预替换字符串

时间:2019-05-05 18:19:52

标签: php regex

我正在使用PHP来使用imap获取电子邮件数据。我想为域添加<a href ="">,为电子邮件添加<a href="mailto:"",以便超链接将显示在电子邮件中。

示例:

This message was created automatically by mail delivery software.
A message that you sent has not yet been delivered to one or more of its
recipients after more than 72 hours on the queue on <a href="http://server.example.com">server.example.com</a>.

The message identifier is:     somerandomid
The subject of the message is: test
The date of the message is:    Sun, 24 Mar 2019 20:37:06 +0000

The address to which the message has not yet been delivered is:

   <a href="mailto:http://subdomain.example.com">subdomain.example.com" target="_blank">failedemail@example.com</a>

No action is required on your part. Delivery attempts will continue for
some time, and this warning may be repeated at intervals if the message
remains undelivered. Eventually the mail delivery software will give up,
and when that happens, the message will be returned to you.

这是我的输出:

This message was created automatically by mail delivery software.
A message that you sent has not yet been delivered to one or more of its
recipients after more than 72 hours on the queue on subdomain.example.com.

The message identifier is:     somerandomid
The subject of the message is: test
The date of the message is:    Sun, 24 Mar 2019 20:37:06 +0000

The address to which the message has not yet been delivered is:

   failedemail@example.com

No action is required on your part. Delivery attempts will continue for
some time, and this warning may be repeated at intervals if the message
remains undelivered. Eventually the mail delivery software will give up,
and when that happens, the message will be returned to you.

当我尝试这样做时:

$body = imap_body($inbox, $email_number, 2);
$email_body = utf8_decode(imap_utf8($body));
$failed_email = getBetween($email_body, 'delivered is:', 'No action');
$message = getBetween($email_body, 'us-ascii', 'returned to you.');
$message = nl2br($message);
$pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
$pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
$replacement = '<a href="mailto:\\1" target="_blank">\\1</a>';
$message = preg_replace('@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', '<a href="http://$1">$1</a>', $message);

echo $message;

它将显示如下输出:

This message was created automatically by mail delivery software.<br />
A message that you sent has not yet been delivered to one or more of     its<br />
recipients after more than 72 hours on the queue on <a href="http://subdomain.example.com">subdomain.example.com</a>.<br />
<br />
The message identifier is:     someid<br />
The date of the message is:    Sun, 21 Apr 2019 18:24:43 +0100<br />
The subject of the message is: Test<br />
<br />
The address to which the message has not yet been delivered is:<br />
<br />
  failedemail@<a href="http://example.com">example.com</a><br />
<br />
No action is required on your part. Delivery attempts will continue for<br />
some time, and this warning may be repeated at intervals if the message<br />
remains undelivered. Eventually the mail delivery software will give up,<br />
and when that happens, the message will be

这是我要实现的目标:

This message was created automatically by mail delivery software.<br />
A message that you sent has not yet been delivered to one or more of     its<br />
recipients after more than 72 hours on the queue on <a href="http://subdomain.example.com" target="_blank">subdomain.example.com</a>.<br />
<br />
The message identifier is:     someid<br />
The date of the message is:    Sun, 21 Apr 2019 18:24:43 +0100<br />
The subject of the message is: Test<br />
<br />
The address to which the message has not yet been delivered is:<br />
<br />
  <a href=mailto"failedemail@example.com" target="_blank">failedemail@example.com</a><br />
<br />
No action is required on your part. Delivery attempts will continue for<br />
some time, and this warning may be repeated at intervals if the message<br />
remains undelivered. Eventually the mail delivery software will give up,<br />
and when that happens, the message will be

您能举例说明如何为域<a href="domaingoeshere" target="_blank"添加href链接subdomain.example.com,以及如何为电子邮件添加href=mailto"emailgoeshere" target="_blank"吗?

谢谢。

0 个答案:

没有答案