Preg_match帮助。无法从电子邮件中读取字符串。 [英] Preg_match Help. Cannot Read String from Email.

查看:249
本文介绍了Preg_match帮助。无法从电子邮件中读取字符串。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,正在读取电子邮件并从电子邮件中提取信息并将其保存到我的sql数据库。但是,它不会插入有关电话号码的任何信息。



这是我的代码来确定电话号码:



如果(preg_match('| ^< b> Phone(。*)> \s *(\S *)<?| U',$ lines [ $ i],$ matches)){
$ phone = trim($ matches [2]);
}

一个电子邮件示例如下:

 名称:Joe Schmoe 

电子邮件地址:joeschmoe@joeschmoe.com

电话:555- 555-5555

以下是电子邮件提供的源代码示例:

 < b>电话:< / b> 555-555-5555< /字体><峰; br> -  

似乎 $ phone 这是空的或空的,因为它没有被插入到数据库中,但是我所有的其他信息都是..



有关这个问题的任何建议?

解决方案

这是一个清理正则表达式,应该为你做的伎俩。它允许数字序列由空格或连字符任意分隔:

  $ re ='% #Rev:20111101 
#在phone:< / br>之后匹配电话号码。
电话:#字面文字:电话:。
\s *#可选(零个或多个)空格。
< / br> #字面文字:< / br>。
\s *#可选的空格。
(#捕获组$ 1:
[0-9] +#{normal +}一个或多个数字
(?:#可选数位分隔符组
[ - ] #{special}数字分隔符
[0-9] +#{normal +}更多一个或多个数字
)*#结束{(特殊正常+)*}构造
)结束$ 1:电话号码。
\s *#可选的空格。
< #确认数字后跟文字<。
%ix'; //使用'x'-free-spacing和'不区分大小写的模式。
if(preg_match($ re,$ lines [$ i],$ matches)){
$ phone = $ matches [1];
}



不要使用 U ungreedy修饰符



使用 U ungreedy修饰符不是最佳实践 - 应该始终避免。当您需要使一个单独的量词懒惰时,只需将修饰符添加到特定的量词。请注意,使用 U 模式修饰符是永远不需要或保证的 - 所有这一切都会使读者感到困惑。



编辑2011-11-01 3:14 PM MDT 断开正则表达式,以自由间隔模式重写,并添加批注o。


I have a script that is reading emails and pulling information out of the email and saving it to my sql database. However, it does not insert any information pertaining to phone numbers.

This is my code to determine the Phone Number:

if (preg_match('|^<b>Phone(.*)>\s*(\S*)<?|U', $lines[$i], $matches)) {
    $phone = trim($matches[2]);
}

An example email would be like this:

Name: Joe Schmoe

E-mail Address: joeschmoe@joeschmoe.com

Phone: 555-555-5555

Here is a Source Sample of what the Email provides:

    <b>Phone:</b> 555-555-5555</font><br> –

It seems the $phone variable ends up being empty or null as it is isn't being inserted in the database but all my other information is..

Any suggestions on this matter?

解决方案

Here is a cleaned up regex that should do the trick for you. It allows digit sequences to be optionally separated by either spaces or hyphens:

$re = '% # Rev:20111101
    # Match phone number after "phone:</br>".
    phone:      # Literal text: "phone:".
    \s*         # Optional (zero or more) whitespace.
    </br>       # Literal text: "</br>".
    \s*         # Optional whitespace.
    (           # Capture group $1:
      [0-9]+    # {normal+} One or more digits.
      (?:       # Group for optional digit separators.
        [ -]    # {special} Digit separator.
        [0-9]+  # {normal+} More one or more digits.
      )*        # End {(special normal+)*} construct.
    )           # End $1: Phone number.
    \s*         # Optional whitespace.
    <           # Ensure number followed by literal "<".
    %ix';       // Use 'x'-free-spacing and 'i'-case-insensitive mode.
if (preg_match($re, $lines[$i], $matches)) {
    $phone = $matches[1];
}

Don't use the U ungreedy modifier!

Using the U ungreedy modifier is NOT best practices - it should always be avoided. When you need to make an individual quantifier lazy, just add the ? modifier to the specific quantifier. Note that using the U mode modifier is never needed or warranted - all it does is serve to confuse the reader.

Edit 2011-11-01 3:14pm MDT "Broke down" regex by rewriting it in free-spacing mode and added lots-o-comments.

这篇关于Preg_match帮助。无法从电子邮件中读取字符串。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆