Skip to content

Instantly share code, notes, and snippets.

@mattattui
mattattui / gist:879249
Created March 21, 2011 10:08
Convert named HTML entities to numeric for XML compatibility
<?php
/* html_convert_entities($string) -- convert named HTML entities to
* XML-compatible numeric entities.
*/
function html_convert_entities($string) {
return preg_replace_callback('/&([a-zA-Z][a-zA-Z0-9]+);/S',
'convert_entity', $string);
}
@mattattui
mattattui / gist:879252
Created March 21, 2011 10:12
Convert HTML entities to XML
<?php
$out = htmlspecialchars(
html_entity_decode($in, ENT_QUOTES, 'UTF-8'),
ENT_QUOTES, 'UTF-8'
);
?>
@mattattui
mattattui / gist:879259
Last active July 11, 2017 08:03
Example PHP cURL function
<?php
// See https://lazycat.org/php-curl.html for license & known issues
// P.S. You'd better have a very good reason for using this instead of http://guzzlephp.org/
function httpGet($url, $ttl = 86400)
{
/* Change this or make it an option as appropriate. If you're
* getting urls that shouldn't be visible to the public, put the
* cache folder somewhere it can't be accessed from the web
*/
@mattattui
mattattui / gist:879267
Created March 21, 2011 10:28
Brief PDO prepared statement snippet
<?php
$query = 'SELECT * FROM my_table WHERE title = :title';
$stmt = $db->prepare($query);
$stmt->bindValue(':title', $title);
@mattattui
mattattui / gist:879269
Created March 21, 2011 10:29
Using prepared statements in PDO
<?php
$db = new PDO('mysql:host=hostname;dbname=defaultDbName',
'username', 'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$query = 'SELECT * FROM my_table WHERE title = :title';
$stmt = $db->prepare($query);
$stmt->bindValue(':title', $myTitle);
$stmt->execute();
@mattattui
mattattui / gist:879273
Created March 21, 2011 10:32
Safe "WHERE … IN" SQL statement in PDO
<?php
$db = new PDO('mysql:host=hostname;dbname=defaultDbName',
'username', 'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$names = array('Alice', 'Bob', 'Charlie');
$values = array_map(array($db,'quote'),$names);
$query = 'SELECT * FROM my_table WHERE name IN ('.join(',',$values).')';
$result = $db->query($query);
@mattattui
mattattui / html 4
Last active September 25, 2015 06:38
Specifying UTF-8 for an HTML file
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>My example page</title>
</head>
@mattattui
mattattui / gist:879289
Created March 21, 2011 10:50
Escaping PHP output in UTF-8
<?php echo htmlspecialchars($my_var, ENT_COMPAT, 'UTF-8'); ?>
@mattattui
mattattui / gist:879290
Created March 21, 2011 10:51
Opening a UTF-8 connection to MySQL with PHP PDO
<?php
try {
$dbh = new PDO('MySQL:host=localhost;dbname=example',
$username, $password,
array(PDO::MySQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
} catch (PDOException $e) {
// You probably want to handle DB failure better than this:
die('Oh no, the database is dead!');
}
?>
@mattattui
mattattui / gist:879294
Created March 21, 2011 10:54
Escaping PHP output
Instead of:
<?php echo $name; ?>
Use:
<?php echo htmlspecialchars($name, ENT_NOQUOTES, 'UTF-8'); ?>