lundi 24 octobre 2016

PHP - Replace digits in a number with other random digits

Problem

  • I'm trying to write a function which replace four digits in a number.
  • Number syntaxis is XX.XXXXXXX (but lenght may vary but min. 8 digits)
  • Digits i want to change always 5th, 6th, 7th and 8th (marked as Z --> XX.XXZZZZXXXX)
  • The number to replace Z, must be random (or 4 random digits in each Z)

Example:

x=12.3456789 --> function --> 12.34xxxx9 where xxxx is a random number with 4 digits or four different digits

Documentation i've read:

What i've tried

Note: i'm started with coding... super rookie here.

function mynumber($x) {
//ok, it's not elegant but here i'm trying to get a number of 4 digits instead of digit by digit
$replacement = var_dump(random_int(1111, 9999));
//I've read about string replace but as the lenght may vary, i don't know how to delimitate the digits i want to change
$x = str_replace($x, $replacement, -?);
}




Aucun commentaire:

Enregistrer un commentaire