#php
15 posts tagged php. Clear filter
-
How to Read and Write to a Single Cell Using PHP and Google Spreadsheets API
This post assumes you've created a project with Google's web console and downloaded a clientsecret.json file from the credentials page. If not, more info is here.....
-
How to Flatten and Merge Arrays in PHP
There is no built in function for taking a multi dimensional array in PHP and flattening it into a single array. However, it's pretty easy to accomplish in a one liner. Here is the code <?php $a =...
-
How to Handle Uploading Really Large Files in PHP
PHP has some default limits that do not work out of the box when you want to work with uploading very large files. So there are a few configuration changes you have to make to the defaults that...
-
How to Extract Time Information from a Natural Language String with a Regular Expression and PHP
You can easily extract time information from a string with a regular expression and PHP (or any language with regular expressions). $str = "Here is how to extract time info.. like 4:30 PM and 2:10...
-
How to Parse Query Strings in PHP
Here is a quick snippet for parsing query strings in PHP. You can use the parseurl and parsestr methods. parsestr will create a variable for you populated with results. $url =...
-
How to Cast an Array to an Object with PHP
It's short and sweet. $objfromarray = (object)['key'='val']; echo $objfromarray-key;
-
Simple Way to Calculate the Odds of Something Happening
Here is a quick way to express a chance of something happening. This is an example in JavaScript that gives you a 1 in 3 chance of being true. [true, false, false].sort(function(){ return...
-
PHP Headers for Sending CSV File Downloads
If you would like to force a file download prompt to the user, instead of just outputting the text to the browser, use the following headers. <?php $filename = "some-filename";...
-
XPath with HTML in PHP One Liner
Here is a one liner for using XPATH with HTML in PHP $doc = new DOMXPath(@DOMDocument::loadHTML(filegetcontents("https://www.reddit.com/r/PHP/"))); Now you can use XPATH to query the html.....
-
How to Calculate Age with PHP
Calculate age in PHP $yourbirthday = '1982-08-29'; $difference = datediff(datecreate(), datecreate($yourbirthday)); $age = $difference-format('%Y'); // 35 Short and sweet
-
How to Use Named Variables with Postgres and PHP PDO Driver
You can write reusable scripts with Postgres by taking advantage of named variables. A named variable starts with a : in your sql script. Here is an example select :anumber You can then use this...
-
How to Get a Random Item from an Array in PHP
Use this snippet for grabbing a random item from an array in php $fruits = ['apple', 'banana', 'carrot', 'date', 'elderberry']; echo arrayrand(arrayflip($fruits)); // = 'banana' PHP's arrayflip...
-
Connect to Postgres on Heroku using DATABASE_URL Config Var with PHP and PDO
Unfortunately PHP's PDO constructor doesn't take a database connection url (in a format that Heroku makes available as a config var) as an argument. It has its own, rather odd syntax. However, it's...
-
How to Slugify a String in PHP
Here is a snippet for generating a URL friendly slug in PHP function slugify($string){ return strtolower(trim(pregreplace('/[^A-Za-z0-9-]+/', '-', $string), '-')); } And you can use it in your code...
-
How to Generate a UUID in PHP 7
Here is a little snippet for generating a UUID in PHP 7 function uuid(){ $data = randombytes(16); $data[6] = chr(ord($data[6]) & 0x0f | 0x40); $data[8] = chr(ord($data[8]) & 0x3f | 0x80); return...