如何提取字符串行以输出电子邮件和消息

时间:2017-12-14 09:12:53

标签: php string email output

我有一个字符串:

  $str = 'xyz@email.com[24ab@gmail.com has mail me]abc@email.Com[i want to...] and others...'; 

在我的成就中,我希望能够使用框内的文本作为电子邮件消息发送到框后面,如:

  $to = 'xyz@email.com';
  $msg = '24ab@gmail.com has mail me';
  $to = 'abc@email.com';
  $msg = 'i want to...';

我真的没有如何在php上运行我只想做一些事情对不起

  foreach($str as $both)
  {
  $to = $both["to"];
  $msg = $both["msg"];

   $to = $email;
   $subject = 'the subject';
   $message = '$msg';
   $headers = 'stringemail@gm.Com';
   $go = mail($to, $subject, $message, $headers);
   if($go)
   {
   echo 'good result';
   }
   }

非常感谢您对我的解决方案的影响

2 个答案:

答案 0 :(得分:1)

你可以explode

$str = 'xyz@email.com[24ab@gmail.com has mail me]abc@email.Com[i want to...]'; 
$str = explode( "]", $str );

foreach ( $str as $value ) {
    $temp = explode( "[", $value );
    if ( count( $temp ) == 2 ) {
        echo "<br /> Email: " . $temp[0];      /* $temp[0] - is the email */
        echo "<br /> Msg: " . $temp[1];        /* $temp[1] - is the message */
        echo "<br />";
    }
}

这将导致:

Email: xyz@email.com
Msg: 24ab@gmail.com has mail me

Email: abc@email.Com
Msg: i want to...

答案 1 :(得分:0)

您可以使用正则表达式,请点击此处https://regex101.com/r/7khpKv/1

$regExpresion = '/(.*?)\[(.*?)\]/';
$str          = 'xyz@email.com[24ab@gmail.com has mail me]abc@email.Com[i want to...]434343@wewe.com[other mesage]';

preg_match_all($regExpresion, $str, $aMatches, PREG_SET_ORDER, 0);


foreach ($aMatches as $aMail)
{
    $to      = $aMail[1];
    $message = $aMail[2];
    $subject = 'the subject';
    $headers = 'From: stringemail@gm.Com';

    $go = mail($to, $subject, $message, $headers);
    if ($go)
    {
        echo 'good result';
    }
}