开发者

find email and replace with a sentence in PHP

How do I prevent people from entering their email address in the description field by replacing their email to some wordings, for example if user entered the following text:

Please contact me via joe.joey@email.com.

I want the output to be:

Please contact me via <email address is blocked>.

I know of a basic str_replace 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 joe.joey@email.com.';
  $lookfor = '@'; 
  $repl开发者_如何学Goacewith = '<email address is blocked>';      
  $newstring = str_replace($lookfor, $replacewith, $string);

thanks.


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);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜