views:

38

answers:

2

Hi,

I have using cakephp and I want to 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.

+5  A: 

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.

Gumbo
+1  A: 
$string = preg_replace('/\d/', 'X', "My id number is 77765444 in Sales dept.");
動靜能量
No, this will simply convert all digits to X, which is not what the OP asked for.
Ben Lee
You mean 1) what if the number is not more than 6 digits and 2) what if there are situations that numbers occur multiple times, such as in `"For some reason there are 2 ID numbers 77765444 in Sales dept."`
動靜能量