Skip to content

Instantly share code, notes, and snippets.

View stemar's full-sized avatar

Steven Marshall stemar

View GitHub Profile
@stemar
stemar / is_blank.php
Last active September 15, 2019 04:43
Check if a value is blank but not zero.
<?php
/**
* Check if a value is blank but not zero.
*
* When you need to accept these as valid, non-empty values:
* - 0 (0 as an integer)
* - 0.0 (0 as a float)
* - "0" (0 as a string)
* Use `@is_blank($a['b']);` to avoid notices.
* @param mixed $value
@stemar
stemar / format.php
Last active December 7, 2019 00:02
PHP format() function with named placeholders
<?php
/**
* Return a formatted string like vsprintf() with named placeholders.
*
* When a placeholder doesn't have a matching key in `$args`,
* the placeholder is returned as is to see missing args.
* @param string $string
* @param array $kwargs
* @param string $pattern
* @return string
@stemar
stemar / varexport.php
Last active December 22, 2022 05:06
PHP var_export() with short array syntax (square brackets) indented 2 spaces.
<?php
/**
* PHP var_export() with short array syntax (square brackets) indented 2 spaces.
*
* NOTE: The only issue is when a string value has `=>\n[`, it will get converted to `=> [`
* @link https://www.php.net/manual/en/function.var-export.php
* @param mixed $expression
* @param bool $return
* @return string
*/
@stemar
stemar / tidy_html5.php
Last active September 15, 2019 04:19
UTF-8 HTML5-compatible Tidy output
<?php
function tidy_html5($html, array $config = [], $encoding = 'utf8') {
$config += [
'doctype' => '<!DOCTYPE html>',
'drop-empty-elements' => 0,
'new-blocklevel-tags' => 'article aside audio bdi canvas details dialog figcaption figure footer header hgroup main menu menuitem nav section source summary template track video',
'new-empty-tags' => 'command embed keygen source track wbr',
'new-inline-tags' => 'audio command datalist embed keygen mark menuitem meter output progress source time video wbr',
'tidy-mark' => 0,
];
@stemar
stemar / long_datetime.php
Last active September 15, 2019 04:16
Format a datetime with a long format
<?php
function long_datetime($datetime, $level = 7) {
$now = new DateTime;
$datetime = new DateTime($datetime);
$suffix = $now > $datetime ? ' ago' : ' ahead';
$diff = $now->diff($dt);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$formats = [
'y' => ' year',
@stemar
stemar / xsd_to_json.php
Created October 19, 2017 22:44
Convert XSD file to JSON file
<?php
function xsd_to_json($xsd_file, $formatted = 448) {
$xml_file = preg_replace('/\.xsd$/', '.xml', $xsd_file);
$json_file = preg_replace('/\.xsd$/', '.json', $xsd_file);
$doc = new DOMDocument();
$doc->preserveWhiteSpace = true;
$doc->load($xsd_file);
$doc->save($xml_file);
$xml = file_get_contents($xml_file);
$obj = str_replace($doc->lastChild->prefix.':', "", $xml);
@stemar
stemar / xml_to_array.php
Last active August 22, 2017 13:52
Convert XML to an array and convert the SimpleXMLElement empty arrays into empty strings.
<?php
function xml_to_array($xml) {
return json_decode(str_replace('{}', '""', json_encode(simplexml_load_string($xml))), TRUE);
}
@stemar
stemar / loadHTML.php
Last active September 23, 2016 03:33
DOMDocument::loadHTML() removing invalid tags without adding DOCTYPE and <html> tag
<?php
// Invalid end tag </s>
$html = <<<HTML
<table>
<tr>
<td>123</s></td>
<td>456</td>
</tr>
</table>
HTML;
@stemar
stemar / xml_to_json.php
Last active September 15, 2019 04:26
Convert XML string to JSON string, formatted or not.
<?php
function xml_to_json($xml, $formatted=TRUE) {
$options = (bool)$formatted ? JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE : 0;
return json_encode(simplexml_load_string($xml), $options);
}