-3

This is my source code:

preg_replace('/[A-Z0-9]/', 'X', number_format('1000000'));

Result: x,xxx,xxx

But, I want the result like 1,xxx,xxx, i.e., don't replace the first number.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
Game Eiei
  • 1
  • 1

3 Answers3

1

Here are a couple of straightforward ways to do it in JavaScript:

var input = "1,000,000"
var output = input.substr(0,1) + input.substr(1).replace(/\d/g, 'x')
console.log(output)

// or:
var output2 = input.replace(/\d/g, function(m, i) { return i ? 'x' : m })
console.log(output2)

(I assume you can convert at least the first one to PHP with no trouble if you'd rather do it server-side, though you did ask for PHP or JS.)

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • i would like to covert in PHP more than JS. Could you help me? – Game Eiei Apr 03 '17 at 04:12
  • If you didn't want JS why did you include it as an option in your question? Sorry, I don't know PHP, but I'm sure it has a `substr()` function, so surely you can convert my first method to PHP? I did originally have the JS string `.slice()` method, but I've now changed it to `.substr()` (which does the same thing) to make it closer to PHP. – nnnnnn Apr 03 '17 at 04:32
  • Thanks for your answer. – Game Eiei Apr 03 '17 at 04:58
0

Please use this code when you are using PHP:

<?php
$number  = '1000000';
$x = number_format($number);
echo $result = str_replace("0", "X", $x);
?>
Shadow
  • 33,525
  • 10
  • 51
  • 64
0

Try this.

echo preg_replace('/(?!^)\S/',  'X', number_format('1000000'));
umair shah
  • 39
  • 3