自动从文档中删除联系信息
-
29-09-2019 - |
题
是否有人知道可以从PHP中使用的好的解决方案,该解决方案将有效地删除电话号码,电子邮件地址甚至是文档的联系地址等联系信息?
更新
大家好,这是我到目前为止想到的,它运行良好。
function sanitizeContent($content)
{
// emails - even containing white space characters like this 't e s t @ ba d . co m'
$content = preg_replace('/([A-Za-x-0-9\s\_\.]{1,50})(?=@)@([A-Za-x-0-9\s\_\.]{1,50})/', '[email removed]', $content);
// urls
$content = preg_replace('/[a-zA-Z]*[:\/\/]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+/i', '[link removed]', $content);
// phone numbers
$content = preg_replace('/(\d)?(\s|-|.|\/)?(\()?(\d){3}(\))?(\s|-|.|\/){1}(\d){3}(\s|-|.|\/){1}(\d){4}/', '[phone removed]', $content);
$content = preg_replace('/[0-9\.\-\s\,\/(x|ext)]{5,50}/', '[phone removed]', $content);
// addresses????
return $content;
}
有人对地址有任何想法吗,我想也许想出一种检测城市的方法,在此之前还剥离X chars。它可能会意外地抓住一些数据,但这可能比披露要好。我真的很想听听其他人是否遇到过。
解决方案
使用正则表达式。
您可以使用preg_replace做到这一点。
$pattern = "/[a-zA-Z]*[:\/\/]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+/i";
$replacement = "[removed]";
preg_replace($pattern, $replacement, $string);
对于电子邮件:
$pattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/";
$replacement = "[removed]";
preg_replace($pattern, $replacement, $string);
对于URL:
不隶属于 StackOverflow