如何模糊textarea字段中的数字

Jos*_*h R 1 php regex cakephp preg-replace

我使用了cakephp,我想隐藏超过六位数的所有数字.

这样的事情

 $string = "My id number is 77765444 in Sales dept.";

 becomes 

 $string = "My id number is XXXXXXXX in Sales dept." 
Run Code Online (Sandbox Code Playgroud)

我感谢任何帮助.

谢谢.

Gum*_*mbo 5

尝试使用preg_replace_callback匿名函数:

$string = preg_replace_callback('/\d{6,}/', function($match) { return str_repeat('X', strlen($match[0])); }, $string);
Run Code Online (Sandbox Code Playgroud)

匿名函数用于将每个出现的六个或更多连续数字替换为相同的数量X.如果您的PHP版本不支持匿名函数(自5.3起可用),请使用普通函数.