dongxi7609 2010-05-02 13:42 采纳率: 0%
浏览 63
已采纳

从文档中删除电话号码

I've got a challenge that I am hoping that the SO community is able to help me with.

I trying to parse a lot of html documents in my PHP application to remove personal details, such as names, addresses and phone numbers. I can remove most of these details without too much trouble, however the phone number is a real problem for me.

My idea is to take the text from these documents and the use a regex to identify the phone numbers and replace them with another value such as 'xxxx'.

I've got 2 regex that I am using one for UK landline numbers and one for UK cell/mobile numbers.

However when I try and run them against the text it just returns an empty string.

I am using the following preg_replace code:

$pattens = array(
        '/^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/',
        '/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/'
    );

$replace = array('xxxxx', 'xxxxx');

//do the search for the numbers.
$updatedContents = preg_replace($pattens, $replace, $htmlContents);

At the moment this is causing me a lot of head scratching as I thought that I had this nailed, but at the moment I can't see what's wrong??

I am sure that it is something really simple.

Thanks,

Grant

  • 写回答

1条回答 默认 最新

  • duan6832168 2010-05-02 13:46
    关注

    You probably don't want to anchor your regular expressions. Remove the ^ from the beginning and the $ from the end.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!