This repository has been archived on 2019-06-21. You can view files and clone it, but cannot push or open issues or pull requests.
sandpiper/utilities/transform.php

44 lines
1.2 KiB
PHP

<?php
/**
* Normalises and removes any unknown characters from a string, making it safe
* for use as a filename.
* @param string $string The string to slugificate.
* @param integer $maxlength The maximum desired length of the resultant string.
* @return string The slugified string.
*/
function slugify($string, $maxlength = 50) {
return substr(preg_replace([
'/\s+/',
'/[^a-z0-9\-_]/'
], [
'-',
''
],
strtolower(trim($string))
), 0, $maxlength);
}
/**
* Escapes a string ready for inclusion in an XML document.
* @param string $string The string to escape.
* @return string The escaped string.
*/
function escape4xml($string) {
return htmlspecialchars($string, ENT_QUOTES | ENT_XML1);
}
/**
* Renders a SimpleXML element node to a pretty-printed XML document.
* @param SimpleXmlElement $simplexml_node The SimpleXML node to render.
* @return string The pretty-printed XML representation of the provided SimpleXML node.
*/
function simplexml_asxml_pretty($simplexml_node) {
libxml_disable_entity_loader(false);
$dom = new DomDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simplexml_node->asXML(), LIBXML_NONET);
return $dom->saveXML();
}