开发者

How to obscure the numbers in the textarea field

开发者 https://www.devze.com 2023-01-22 11:23 出处:网络
I have using cakephp and I want to 开发者_C百科obscure all numbers which is more than six digits.

I have using cakephp and I want to 开发者_C百科obscure all numbers which is more than six digits.

something like this

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

 becomes 

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

I appreciate any help.

Thanks.


Try this using preg_replace_callback and an anonymous function:

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

The anonymous function is used to replace each occurrence of six or more consecutive digits by the same amount of X. If your PHP version does not support anonymous functions (available since 5.3), use a normal function instead.


$string = preg_replace('/\d/', 'X', "My id number is 77765444 in Sales dept.");
0

精彩评论

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