2017-03-05 1 views
2

このような文字列の部分を隠す必要があります2345 *** 47563 **** 3432。私はPHPのsubstr_replaceを以下のように使用していますが、入れ子になったsubstr_replaceを使うのではなく、より良いやり方があるかどうかを考えています。PHPを使用して異なる位置の文字列の一部を効率的に置換する方法はありますか?

$mtn = 2348764756783432; 
substr_replace(substr_replace($mtn, '***', 3,3), '***', 9, 3) 
The result is 234***475***3432. 
+0

改善されたタイトルや言語が完了 –

+0

@JorisMeysそれをより明確にします。今はっきりしていると思います。お返事 –

答えて

0

あなたはこれを試すことができます。

preg_replace('/(\d{3})(\d){3}/', '$1***', $mtn); 

しかし、私はこの1つは読みするのが最も簡単であることを確認していません。あなたはこの1つを述べたように
もが、別のパフォーマンスを持っている:

$mtn = 2348764756783432; 

$time = microtime(true); 
$result = substr_replace(substr_replace($mtn, '***', 3,3), '***', 9, 3); 
printf('Result: %s, Spent time: %f %s', $result, microtime(true) - $time, PHP_EOL); 

$time = microtime(true); 
$result = preg_replace('/(\d{3})(\d){3}/', '$1***', $mtn); 
printf('Result: %s, Spent time: %f %s', $result, microtime(true) - $time, PHP_EOL); 

出力:

Result: 234***475***3432, Spent time: 0.000009 
Result: 234***475***3432, Spent time: 0.000092 
+0

ありがとう@Vladimir、それを試してみるが、substrがpreg_replaceより速いどこかのオンラインを読む –

+0

@CharlesOkaformbahはい、それは本当です!しかし、別の選択肢、おそらく最高ではないが、別のオプションです!) –

関連する問題