查找并编辑字符串中的电子邮件地址

发布于 2024-11-24 15:26:15 字数 816 浏览 5 评论 0原文

如何通过用一些单词替换电子邮件地址来防止人们在描述字段中显示他们的电子邮件地址?例如,如果用户输入以下文本:

Please contact me via [email protected].

我希望输出为:

Please contact me via <email address is blocked>.

我知道基本的 str_replace() 函数,但输出只是:

//output is Please contact me via joe.joey <email address is blocked> email.com
$string = 'Please contact me via [email protected].';
$lookfor = '@'; 
$replacewith = '<email address is blocked>';      
$newstring = str_replace($lookfor, $replacewith, $string);

How do I prevent people from presenting their email address in the description field by replacing their email with some words? For example, if a user entered the following text:

Please contact me via [email protected].

I want the output to be:

Please contact me via <email address is blocked>.

I know of a basic str_replace() function, but the output would simply be:

//output is Please contact me via joe.joey <email address is blocked> email.com
$string = 'Please contact me via [email protected].';
$lookfor = '@'; 
$replacewith = '<email address is blocked>';      
$newstring = str_replace($lookfor, $replacewith, $string);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尬尬 2024-12-01 15:26:15

这是使用 preg_replace 的最佳时机。我在这里稍微简化了有效电子邮件的要求(电子邮件可能非常复杂),但类似于:

$newstring = preg_replace("/[\w-]+@([\w-]+\. )+[\w-]+/", $replacewith, $string);

This is a perfect time to use preg_replace. I've slightly simplified the requirements for a valid email here (emails can be horridly complex), but something like:

$newstring = preg_replace("/[\w-]+@([\w-]+\.)+[\w-]+/", $replacewith, $string);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文