为了账号安全,请及时绑定邮箱和手机立即绑定

PHP 特定字符之前和之后的子字符串,直到空格、逗号...?

PHP 特定字符之前和之后的子字符串,直到空格、逗号...?

PHP
德玛西亚99 2023-12-15 16:15:17
我正在尝试抓取一封电子邮件。假设我有一个很长的字符串:$str = "this is the email query@stackoverflow.com which you might want to scrape";$needle_1 = "@";$needle_2 = array("[ ]","[,]","["]");我想提取电子邮件。我认为使用@是解决方案。 所以基本上,我需要获取@之前和之后的所有字符,直到空格、逗号或"。目前最好的试用是:$string = 'this is the email query@stackoverflow.com which you might want to scrape';$template = "/[\w]+[@][\w]+[.][\w]+/";preg_match_all($template, $string, $matches);var_dump($matches);我怎样才能做到这一点?
查看完整描述

2 回答

?
千巷猫影

TA贡献1829条经验 获得超7个赞

我认为它看起来更好


  $string = 'this is the email query@stackoverflow.com which you might want to scrap';

  $template = "/[\w]+[@][\w]+[.][\w]+/";

  preg_match_all($template, $string, $matches);

  var_dump($matches);


查看完整回答
反对 回复 2023-12-15
?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

您应该为此创建一个正则表达式:


<?php 

    $str = "this is the email query@stackoverflow.com which you might want to scrap";

    $pattern = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';

    preg_match_all($pattern, $str, $matches);

    var_dump($matches[0]);

?>


查看完整回答
反对 回复 2023-12-15
  • 2 回答
  • 0 关注
  • 72 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信