ComunicAPI/functions/strings.php

35 lines
862 B
PHP
Raw Permalink Normal View History

2017-05-17 12:43:12 +00:00
<?php
/**
2017-06-23 17:00:40 +00:00
* Strings methods
2017-05-17 12:43:12 +00:00
*
* @author Pierre HUBERT
*/
/**
* Generate a random string, using a cryptographically secure
* pseudorandom number generator (random_int)
*
* @param int $length How many characters do we want?
* @param string $keyspace A string of all possible characters
* to select from
* @return string
*/
2017-12-29 08:46:10 +00:00
function random_str(int $length, string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
2017-05-17 12:43:12 +00:00
{
$str = '';
$max = mb_strlen($keyspace, '8bit') - 1;
for ($i = 0; $i < $length; ++$i) {
$str .= $keyspace[random_int(0, $max)];
}
return $str;
2017-12-29 08:46:10 +00:00
}
/**
* Get the current date formatted for the "datetime" object of
* a Mysql database
*
* @return string Formatted string
*/
function mysql_date() : string {
return date("Y-m-d h:i:s", time());
2017-06-23 17:00:40 +00:00
}