Convert JSON Array to PHP Array

Posted by & filed under JSON, PHP.

To convert a JSON array into a PHP array, use the following script: <?php $json = file_get_contents(‘json/yourscript.json’); // Get the JSON data $phpObj = json_decode($json,true); // Convert to PHP Object print_r($phpObj); // Convert to PHP Array ?> file_get_contents(‘json/yourscript.json’); Allows you to get the JSON data easily. json_decode($json,true); Is a PHP function that converts your JSON… Read more »

Get only the Domain Name from HTTP_REFERRER in PHP

Posted by & filed under PHP.

$_SERVER[‘HTTP_REFERER’] gives you the URL from where the user came, which is the user’s previous page (whatever page that would appear if the user clicked “Back” in their browser) But $_SERVER[‘HTTP_REFERER’]  gives you the complete URL, something like this : driverstest.info/test/quiz.php?quizgroup=7 If you just want get the domain name .i.e. driverstest.info from the full URL, then you can… Read more »

Convert Month Numbers to Month Names in PHP

Posted by & filed under PHP.

Here is a lovely bit of code to help you convert months in numbers to proper names! <?php $monthNum = 11; $monthName = date(“F”, mktime(0, 0, 0, $monthNum, 10)); echo $monthName; // Outputs: November ?>

Dealing with apostrophes and quotation marks for forms and output with PHP

Posted by & filed under PHP.

I have run into the same annoying problem for a long time and finally found an easy solution. Whenever forms are submitted via PHP (by a user or myself) and apostrophes or quotation marks are added in forms, they are escaped using mysqli_real_escape_string and entered into the database without a hitch. However, when outputting the data… Read more »

How to keep the carriage return in textarea using PHP?

Posted by & filed under MySQL, PHP.

I’ve created submission forms with the textarea box, but when I echoed out the submission, all the carriage returns disappeared! Of course you don’t want to force the user to type <br /> or <p> tags into the posts. Actually, I learned that the carriage returns did not disappear, you simply need to add a… Read more »

Find a needle in a haystack with PHP in_array

Posted by & filed under PHP.

Want to see if a single item is in an array in PHP? Use in_array. For example, you have an array of colors and you want to find if red exists. Try this… $array = array(red, blue, green); if (in_array(“red”, $array)) { echo “has red”; }