Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How do I generate random 12 character char for URL of a page for the record in the database like how youtube does it with 11 characters https://www.youtube.com/watch?v=kDjirnJcanw
I want it to be unique for each entry in the database and consist of letters of upper and lower case, numbers and maybe (if not bad for security) even special characters.
Function:
function generateRandomString($length = 12) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
Then call it:
$randomString = generateRandomString();
Slightly adapted from Stephen Watkins' solution here
This is a pretty naive approach, but you could define an array of allowed characters, and just randomly select N:
function randomString($length) {
$string = '';
$characters = "123456789abcdefghijklmnopqrstuvwxyzABCDEFHJKLMNPRTVWXYZ";
$randMax = strlen($characters)-1;
for ($i = 0; $i < $length; ++$i) {
$string .= $characters[mt_rand(0, randMax)];
}
return $string;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to generate 34 random letters (capitalized and not) and numbers. Before the generated 34 random alphanumerics there is a word pro_sec_ which is there everytime it generate. And the 25th & 26th is 00
So the generated result will be: (just an example)
pro_sec_Vy6Q67sh6HvP90j7gWnq6SxN00p8Gapu66
Another ex.
pro_sec_6B57nTshusbnay6esjabxns800nGvaz2Lov
The pro_sec_ and the 00 there in 25th and 26th are always the same and the rest are just generated.
This should work for you:
<?php
function getToken($n) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $n; $i++) {
$index = rand(0, strlen($characters) - 1);
$randomString .= $characters[$index];
}
return $randomString;
}
echo 'pro_sec_' . getToken(24) . '00' . getToken(9);
Performant solution without using any loops or calling the function twice:
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$split = str_split(substr(str_shuffle($chars), 0, 32), 25);
$res = 'pro_sec_'.$split[0].'00'.$split[1];
echo $res; // pro_sec_c8s5iwyQY0nZoJf73EOkC9vqS00dNLWVKg
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Hi there I was wondering if I could create an array that accept a sentence and turns it into an array for example
$sentence = 'my name is john'
then the array will be:
$arr = {my name is john,
my name is, my name john, my is john, name is john,
my name, my is, my john, name is, name john, is john,
my, name, is, john}
anyone could help me with the implementation in any kind of loop that would be great because im currently creating a simple search engine algorithm thx :D
Think of it as a n-bit integer, with each bit corresponding to whether a word is included or not in a given string in your array. Loop from 1 to (1 << n) - 1, which in this case is 1 to 15, to get your 15 word lists, and for each one, check each of the bits in the integer and add the corresponding word if the corresponding bit is set:
function getCombinations($sentence)
{
$words = explode(" ", $sentence);
$combinations = array();
for($i = 1; $i < (1 << count($words)); $i++)
{
$wordlist = "";
for($j = 0; $j < count($words); $j++)
{
if($i & (1 << $j))
{
$wordlist = $wordlist . " " . $words[$j];
}
}
array_push($combinations, substr($wordlist, 1));
}
return $combinations;
}
$a = "my name is john";
print_r(getCombinations($a));
If you want your strings to be sorted by the number of words, add an extra loop for the word counts:
for($wordcount = count($words); $wordcount >= 1; $wordcount--)
{
for($i = 1; $i < (1 << count($words)); $i++)
{
if(NumberOfSetBits($i) == $wordcount)
{
$wordlist = "";
// generate word list..
}
}
}
NumbeOfSetBits function from here
This question already has answers here:
PHP random string generator
(68 answers)
Closed 7 years ago.
I want to generate a 6 character long unique key in php in which first 3 should be alphabets and next 3 should be digits.
I know about the uniqid() function but it generates 13 characters long key and also it wont fit in my requirement as I need first 3 characters as alphabets and next 3 as numbers.
Any way in which I can modify uniqid() to fit in my requirements?
I also dont want any collisions because if that happens my whole database will be wasted that is why I can't use rand function because it is very likely that I will get collisions
You could create a manual randomizer like this:
<?php
$alphabet = 'abcdefghijklmnopqrstuvwxyz';
$numbers = '0123456789';
$value = '';
for ($i = 0; $i < 3; $i++) {
$value .= substr($alphabet, rand(0, strlen($alphabet) - 1), 1);
}
for ($i = 0; $i < 3; $i++) {
$value .= substr($numbers, rand(0, strlen($numbers) - 1), 1);
}
The $value variable will then be a string like "axy813" or "nbm449".
<?php
$alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$numbers = "1234567890";
$randstr = '';
for($i=0; $i < 6; $i++){
if($i<3){
$randstr .= $alphabets[rand(0, strlen($alphabets) - 1)];
} else {
$randstr .= $numbers[rand(0, strlen($numbers) - 1)];
}
}
echo $randstr;
?>
this will do the work for you
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I tried to make a simple name generator. Help me to identify why it's not working. I think it's because of mt_rand function. Sorry if question seems banal or irrelevant, I'm first time here and new to programming. Here's the code:
<?php
echo 'Ovo je moja verzija Polumenta generatora';
$prvo = 'bcdfghtnjpknmlrjdzdjs';
$drugo = 'aeiou';
$trece = 'bcdfghtnjpknmlrjsdzdj';
$cetvrto = 'uo';
$prvos = mt_rand($prvo[0],$prvo[17]);
$drugos = mt_rand($drugo[0],$drugo[4]);
$treces = mt_rand($trece[0],$trece[17]);
$cetvrtos = mt_rand($cetvrto[0],$cetvrto[1]);
echo $prvos.$drugos.$treces.$cetvrtos.' Polumenta'
?>
mt_rand takes integers as arguments and returns an integer. You are trying to pass it characters and return characters. You should do something like:
<?php
echo 'Ovo je moja verzija Polumenta generatora';
$prvo = 'bcdfghtnjpknmlrjdzdjs';
$drugo = 'aeiou';
$trece = 'bcdfghtnjpknmlrjsdzdj';
$cetvrto = 'uo';
$prvos = $prvo[mt_rand(0,strlen($prvo) - 1)];
$drugos = $drugo[mt_rand(0,strlen($drugo) - 1)];
$treces = $trece[mt_rand(0,strlen($trece) - 1)];
$cetvrtos = $cetvrto[mt_rand(0,strlen($cetvrto) - 1)];
echo $prvos.$drugos.$treces.$cetvrtos.' Polumenta'
?>
You can make it in another way, using arrays and rand() function.
$chrs[0] = str_split('bcdfghtnjpknmlrjdzdjs'); // array of consonants
$chrs[1] = str_split('aeiou'); // array of vowels
$length = 8; // nick name length
Than generate random sequence of chars any length
for($i = 0; $i < $length; $i++)
{
$v_or_c = rand(0,1);
if($v_or_c)
{
$nick_name .= $chrs[$v_or_c][rand(0, sizeof($chrs[$v_or_c]))];
}
else
{
$nick_name .= $chrs[$v_or_c][rand(0, sizeof($chrs[$v_or_c]))];
}
}
echo ucfirst($nick_name); // ucfirst - to upper case first letter
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Kindly someone explain to me about this?
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a";
is faster than
foreach($aHash as $key=>$val) $aHash[$key] .= "a";
According to The PHP Benchmark. However I have a a code in my script:
My CODE:
foreach($_SESSION['undo'] as $key2=>$value2)
{
if{
}
else
{
}
.
.
.
.
}
How can I convert a code like that as shown above to my code?
Kindly explain why? Thank you.
do not count in for condition
you can try this
$size = count($_SESSION['undo']);
for($i = 0; $i< $size; $i++){
$value = $_SESSION['undo'][$i];
}
In a foreach loop the first part is your array and the second part after the as is the current value when iterating.
When using a for loop you are working with indexes and have to manually access them. Just do the same as in your example. I'm assuming you are working with an associative array as you are using array keys.
$myArray = $_SESSION['undo'];
$keys = array_keys($myArray);
$size = sizeOf($keys);
for ($i = 0; $i < $size; $i ++) {
/* do something with $myArray[$keys[$i]] */
echo $myArray[$keys[$i]];
}
try this:
for($i = 0; $i< count($_SESSION['undo']); $i++){
$value = $_SESSION['undo'][$i];
}
Seems you made a mistake, from the http://www.phpbench.com/,
$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a";
This way cost 92 us, just foreach cost 16 us, with 'as' cost 21 us.
Guys, wake up....