Created
April 30, 2012 10:55
-
-
Save goldsky/2557346 to your computer and use it in GitHub Desktop.
This class has trim utilities to overcome the TinyMCE's bug related to the whitespace characters (Â| ).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @license The included classes have their own licenses | |
* @copyright (c) 2012 by goldsky <[email protected]> | |
*/ | |
class Utility { | |
public function __construct() {} | |
/** | |
* View any string as a hexdump. | |
* | |
* This is most commonly used to view binary data from streams | |
* or sockets while debugging, but can be used to view any string | |
* with non-viewable characters. | |
* | |
* @version 1.3.2 | |
* @author Aidan Lister <[email protected]> | |
* @author Peter Waller <[email protected]> | |
* @link http://aidanlister.com/2004/04/viewing-binary-data-as-a-hexdump-in-php/ | |
* @param string $data The string to be dumped | |
* @param bool $htmloutput [default: true] Set to false for non-HTML output | |
* @param bool $uppercase [default: false] Set to true for uppercase hex | |
* @param bool $return [default: false] Set to true to return the dump | |
*/ | |
public function hexdump($data, $htmloutput = true, $uppercase = false, $return = false) { | |
// Init | |
$hexi = ''; | |
$ascii = ''; | |
$dump = ($htmloutput === true) ? '<pre>' : ''; | |
$offset = 0; | |
$len = strlen($data); | |
// Upper or lower case hexadecimal | |
$x = ($uppercase === false) ? 'x' : 'X'; | |
// Iterate string | |
for ($i = $j = 0; $i < $len; $i++) { | |
// Convert to hexidecimal | |
$hexi .= sprintf("%02$x ", ord($data[$i])); | |
// Replace non-viewable bytes with '.' | |
if (ord($data[$i]) >= 32) { | |
$ascii .= ($htmloutput === true) ? | |
htmlentities($data[$i]) : | |
$data[$i]; | |
} else { | |
$ascii .= '.'; | |
} | |
// Add extra column spacing | |
if ($j === 7) { | |
$hexi .= ' '; | |
$ascii .= ' '; | |
} | |
// Add row | |
if (++$j === 16 || $i === $len - 1) { | |
// Join the hexi / ascii output | |
$dump .= sprintf("%04$x %-49s %s", $offset, $hexi, $ascii); | |
// Reset vars | |
$hexi = $ascii = ''; | |
$offset += 16; | |
$j = 0; | |
// Add newline | |
if ($i !== $len - 1) { | |
$dump .= "\n"; | |
} | |
} | |
} | |
// Finish dump | |
$dump .= $htmloutput === true ? | |
'</pre>' : | |
''; | |
$dump .= "\n"; | |
// Output method | |
if ($return === false) { | |
echo $dump; | |
} else { | |
return $dump; | |
} | |
} | |
/** | |
* Trim array values | |
* @param array $array array contents | |
* @param string $charlist [default: null] defined characters to be trimmed | |
* @link http://php.net/manual/en/function.trim.php | |
* @return array trimmed array | |
*/ | |
public function trimArray($input, $charlist = null) { | |
if (is_array($input)) { | |
$output = array_map(array($this, 'trimArray'), $input); | |
} else { | |
$output = $this->trimString($input, $charlist); | |
} | |
return $output; | |
} | |
/** | |
* Trim string value | |
* @param string $string source text | |
* @param string $charlist defined characters to be trimmed | |
* @link http://php.net/manual/en/function.trim.php | |
* @return string trimmed text | |
*/ | |
public function trimString($string, $charlist = null) { | |
if (empty($string)) { | |
return ''; | |
} | |
$string = htmlentities($string); | |
// blame TinyMCE! | |
$string = preg_replace('/(Â| )+/i', '', $string); | |
$string = trim($string, $charlist); | |
$string = trim(preg_replace( '/\s+^(\r|\n|\r\n)/', ' ', $string )); | |
return $string; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment