查找并编辑字符串中的电子邮件地址
如何通过用一些单词替换电子邮件地址来防止人们在描述字段中显示他们的电子邮件地址?例如,如果用户输入以下文本:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是使用 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);