如何从PHP中的字符串中删除电子邮件地址和链接? [英] How to remove email addresses and links from a string in PHP?

查看:126
本文介绍了如何从PHP中的字符串中删除电子邮件地址和链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从字符串中删除所有的电子邮件地址和链接,并将其替换为[已删除],我有点迷失如何做。

有人可以帮助我吗?

谢谢。

i need to remove all email addresses and links from a string and replace them with "[removed]" and i'm a bit lost on how to do it.
Can someone help me on this?
Thanks.

推荐答案

您可以使用preg_replace来执行。

You can use preg_replace to do it.

电子邮件:

$pattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/";
$replacement = "[removed]";
preg_replace($pattern, $replacement, $string);

为网址:

$pattern = "/[a-zA-Z]*[:\/\/]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+/i";
$replacement = "[removed]";
preg_replace($pattern, $replacement, $string);

资源

PHP手册条目: http://php.net/manual/en/function .preg-replace.php

信用到期的信用额度:从preg_match联机帮助页面获取的电子邮件正则表达式,以及从 http://www.weberdev.com/get_example-4227.html

Credit where credit is due: email regex taken from preg_match manpage, and URL regex taken from: http://www.weberdev.com/get_example-4227.html

这篇关于如何从PHP中的字符串中删除电子邮件地址和链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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