8

How can i add a single random character (0-9 or a-z or - or _) at a random place in a string.

I can get the random position by following:

$random_position = rand(0,5);

Now How can i get a random number ( 0 to 9 ) OR random character (a to z) OR (-) OR (_)

and finally how i can add character to the above string in the above random position.

For example following is string:

$string = "abc123";
$random_position = 2;
$random_char = "_";

the new string should be:

"a_bc123"
1
  • Really? And what is the difference between position 0 and 1? Commented Jun 28, 2011 at 7:47

5 Answers 5

5
$string = "abc123";
$random_position = rand(0,strlen($string)-1);
$chars = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789-_";
$random_char = $chars[rand(0,strlen($chars)-1)];
$newString = substr($string,0,$random_position).$random_char.substr($string,$random_position);
echo $newString;
Sign up to request clarification or add additional context in comments.

5 Comments

This character map includes capital letters as well, which the OP didn't seem to indicate.
Well, it does, but the map can be safely edited as per requirement.
@Shef, yes i did not want to include capitals, but i removed them.
Please have a look at @rrapuya's use of substr_replace() - Pretty nice replacesment for the substr() usage.
@Phliplip, Thanks. i surely did.
2

try something like this

<?php 

   $orig_string = "abc123";
   $upper =strlen($orig_string);
   $random_position = rand(0,$upper);
   $int = rand(0,51);
   $a_z = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
   $rand_char = $a_z[$int];


   $newstring=substr_replace($orig_string, $rand_char, $random_position, 0);

   echo 'original-> ' .$orig_string.'<br>';
   echo 'random-> ' .$newstring;
?>

2 Comments

I like your solution, you're the only one that thought of using substr_replaces with a zero length. From the manual; "Of course, if length is zero then this function will have the effect of inserting replacement into string at the given start offset." But you are missing the digits, _ and - in the characterset, and I don't think sunjie wants the uppercase included. Also your rand() should utilize strlen() like the other answers.
well havent think about utilizing strlen() in rand() i just realized when i get to read their answer.
1
$string = 'abc123';
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789-_';
$new_string = substr_replace(
    $string,
    $chars[rand(0, strlen($chars)-1)],
    rand(0, strlen($string)-1),
    0
);

Comments

0

get the string length:

$string_length = strlen($string);//getting the length of the string your working with
$random_position = 2;//generate random position

generate the "random" character:

$characters = "abcd..xyz012...89-_";//obviously instead of the ... fill in all the characters - i was just lazy.

getting a random character out of the character string:

$random_char = substr($characters, rand(0,strlen($characters)), 1);//if you know the length of $characters you can replace the strlen with the actual length

breaking the string into 2 parts:

$first_part = substr($string, 0, $random_position);
$second_part = substr($string, $random_position, $string_length);

adding the random character:

$first_part .=  $random_char;

combining the two back together:

$new_string = $first_part.$second_part;

this may not be the best way, but I think it should do it...

Comments

0
// map of characters
$map = '0123456789abcdefghijklmnopqrstuvwxyz-_';
// draw a random character from the map
$random_char_posotion = rand(0, strlen($map)-1); // say 2?
$random_char = $map[$random_char_posotion]; // 2

$str = 'abc123';

// draw a random position
$random_position = rand(0, strlen($str)-1); // say 3?

// inject the randomly drawn character
$str = substr($str, 0, $random_position).$random_char.substr($str,$random_position);

// output the result
echo $str; // output abc2123

1 Comment

Would a $random_char_position of 2 not indicate that $random_char became '2' ? Just saying ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.