2

I want to generate a random string of 50 characters string using Letters, Numbers and Special Symbols.

Below Code is working Fine for me.

But the issue is that if I add Special Symbol <

I does not work as it should be

Kindly help.

<?php
$complete_string = NULL;
$generated_string = NULL;
$generated_string_length = 50;

$lower_case_string = 'abcdefghijklmnopqrstuvwxyz';
$upper_case_string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$numbers_string = '0123456789';
$special_symbols_string = ',./?;:"[]{}+=-_)(*&^%$#@!~`>'; // All Special Symbols without <

$complete_string = $complete_string.$lower_case_string.$upper_case_string.$numbers_string.$special_symbols_string;

$generated_string = substr(str_shuffle(str_repeat($complete_string, ceil($generated_string_length/strlen($complete_string)) )),1,$generated_string_length);

echo $generated_string;
?>
9
  • This code works both with and without '<'. Could you explain the problem? Commented Oct 4, 2017 at 8:01
  • Above Code is working fine as it generate 50 length random string, but if add special symbol < , it does not work then. Commented Oct 4, 2017 at 8:06
  • I tried to execute this on php 5.6 and php 7.1 with '<', every time I received a string, sometimes containing '<', with length of 50 symbols Commented Oct 4, 2017 at 8:16
  • i need every time bro :) not sometimes :) Commented Oct 4, 2017 at 8:20
  • You can't recieve all symbols in your result every time, because your $complete_string is 91 symbols length, then you shuffle it and crop to 50 symbols. So you always loose 41 random symbol. Commented Oct 4, 2017 at 8:36

1 Answer 1

1
function randstr ($len=10, $abc="abcdefghijklmnopqrstuvwxyz,./?;:[]{}+=-_)(*&^%$#@!~`>") {
    $letters = str_split($abc);
    $str = "";
    for ($i=0; $i<=$len; $i++) {
        $str .= $letters[rand(0, count($letters)-1)];
    };
    return $str;
};

echo randstr(50);
Sign up to request clarification or add additional context in comments.

1 Comment

$abc = "dsadsad&*^%*^%^" as set your code as you want in random strings

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.