Skip to main content

Posts

Showing posts from December 14, 2022

How to Generate PHP Random String Token (8 Ways)

  Generating random string token may sound like a trivial work. If you really want something “truly random”, it is one difficult job to do. In a project where you want a not so critical scrambled string there are many ways to get it done. I present eight different ways of getting a random string token using PHP. Using  random_int() Using  rand() By string shuffling to generate a random substring. Using  bin2hex() Using  mt_rand() Using hashing  sha1() Using hashing  md5() Using PHP  uniqid() There are too many PHP functions that can be used to generate the random string. With the combination of those functions, this code assures to generate an unrepeatable random string and unpredictable by a civilian user. 1) Using  random_int() The PHP  random_int()  function generates cryptographic pseudo random integer. This random integer is used as an index to get the character from the given string base. The string base includes 0-9, a-z and ...

How to Read a CSV to Array in PHP

  There are many ways to read a CSV file to an array. Online hosted tools provide interfaces to do this. Also, it is very easy to create a custom user interface for the purpose of reading CSV to the array. In PHP, it has more than one native function to read CSV data. fgetcsv() – It reads the CSV file pointer and reads the line in particular to the file handle. str_getcsv() -It reads the input CSV string into an array. This article provides alternate ways of reading a CSV file to a PHP array. Also, it shows how to prepare HTML from the array data of the input CSV. Quick example This example reads an input CSV file using the PHP fgetcsv() function. This function needs the file point to refer to the line to read the CSV row columns. <?php // PHP function to read CSV to array function csvToArray ( $csv ) { // create file handle to read CSV file $csvToRead = fopen ( $csv , 'r' ); // read CSV file using comma as delimiter while (! feof ( $csvToRead )) { ...