42 lines
1.2 KiB
PHP
42 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) {
|
|
var_dump($string);
|
|
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) {
|
|
$dom = dom_import_simplexml($simplexml_node);
|
|
$dom->preserveWhitespace = false;
|
|
$dom->formatOutput = true;
|
|
return $dom->saveXML();
|
|
}
|