Terminology
array() ¶ Create an array.
date('H:i:s') ¶ definition: 'Current time in 00:00:00 format.'
date('Y/m/d') ¶ Current date in 0000/00/00 format.
debug_backtrace() ¶ Generates a backtrace into an array.
debug_print_backtrace() ¶ Prints a backtrace.
get_class_methods(<object>) ¶ definition: 'List the class methods of <object> into an array.'
get_class(<object>) ¶ definition: 'Get the name of the class of <object>.'
implode(<array>,<string>) ¶ Reduce an array to a string.
list() ¶ Assign variables as if they were an array.
preg_match ¶ definition: 'Perform a regular-experssion match.'
strtotime(<string>) ¶ definition: 'Convert date string or time length string to Unix timestamp.'
Facts, Thoughts and Opinions
Convert PHP object to associative array
Convert PHP object to associative array by casting it with the array keyword.
$array = (array) $object;
Create a PHP object without a class definition.
$x = (object) array('field1'=>'A', 'field2'=>'B');
Detect malicious code in a SQL statement in PHP
function detectMalice($sql) { return preg_match("/;\s*(ALTER|CREATE|DELETE|DROP|EXEC|INSERT|MERGE|SELECT|UPDATE)/",$sql); } function test($sql) { if (detectMalice($sql)) { echo "MALICIOUS"; } else { echo "GOOD"; } echo ": $sql "; } test("SELECT * FROM mytable"); test("SELECT * FROM mytable;"); test("SELECT * FROM mytable;DELETE FROM mytable"); test("SELECT * FROM mytable; DROP mytable"); test("SELECT * FROM mytable; DROP mytable");
Download file directly.
Example for a PDF file.
header("Content-Type: application/pdf");
header('Content-Disposition: attachment; filename="temp.pdf"');
header("Content-Length: ".strlen($pdf));
echo $pdf;
flush();
exit();
htmlToString()
In PHP, this is the easiest way to get a block of HTML into a string.
public function htmlToString() { ob_start(); ?> <!-- Your HTML goes here. --> /* Or 'echo' PHP strings or even function results here. */ $html = ob_get_contents(); ob_end_clean(); return $html; }
Basically, whatever is echoed between the ob_start() and $html = statements goes into the string.
PHP implode() to make INSERT statement
<?php
// array containing data
$array = array(
"name" => "John",
"surname" => "Doe",
"email" => "vog.ecnegilletni|eod.j#vog.ecnegilletni|eod.j"
);
// build query…
$sql = "INSERT INTO table";
// implode keys of $array…
$sql .= " (`".implode("`, `", array_keys($array))."`)";
// implode values of $array…
$sql .= " VALUES ('".implode("', '", $array)."') ";
// execute query…
$result = mysql_query($sql) or die(mysql_error());
?>
PHP Magic Constants
__LINE__
__FILE__
__FUNCTION__
__CLASS__
__METHOD__
Images
- Subtopics
- Date & Time Manipulation in PHP
- PHP Arrays
- Writings
Sources & Bookmarks