Skip to content

Instantly share code, notes, and snippets.

@goldsky
Created April 30, 2012 10:55

Revisions

  1. goldsky revised this gist Oct 15, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion utility.class.php
    Original file line number Diff line number Diff line change
    @@ -118,7 +118,7 @@ public function trimString($string, $charlist = null) {
    // blame TinyMCE!
    $string = preg_replace('/(Â| )+/i', '', $string);
    $string = trim($string, $charlist);
    $string = trim( preg_replace( '/\s+/', ' ', $string ) );
    $string = trim(preg_replace( '/\s+^(\r|\n|\r\n)/', ' ', $string ));
    return $string;
    }

  2. goldsky revised this gist Oct 15, 2012. 1 changed file with 20 additions and 33 deletions.
    53 changes: 20 additions & 33 deletions utility.class.php
    Original file line number Diff line number Diff line change
    @@ -88,51 +88,38 @@ public function hexdump($data, $htmloutput = true, $uppercase = false, $return =

    /**
    * 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
    * @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(array $array, $charlist = null) {
    $newArray = array();
    foreach ($array as $k => $v) {
    if (is_array($v)) {
    $array[$k][$v] = $this->trimArray($v, $charlist);
    } else {
    $val = $this->trimString($v, $charlist);
    if (empty($v)) {
    continue;
    }
    $newArray[$k] = $val;
    }
    public function trimArray($input, $charlist = null) {
    if (is_array($input)) {
    $output = array_map(array($this, 'trimArray'), $input);
    } else {
    $output = $this->trimString($input, $charlist);
    }
    sort($newArray);

    return $newArray;
    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
    * @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);
    if ($charlist === null) {
    $string = trim($string);
    } else {
    $string = trim($string, $charlist);
    }

    if (empty($string)) {
    return FALSE;
    }

    $string = trim($string, $charlist);
    $string = trim( preg_replace( '/\s+/', ' ', $string ) );
    return $string;
    }

    }
    }
  3. goldsky revised this gist Apr 30, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion utility.class.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php

    /**
    * @license The include classes have their own licenses
    * @license The included classes have their own licenses
    * @copyright (c) 2012 by goldsky <goldsky@fastmail.fm>
    */
    class Utility {
  4. goldsky revised this gist Apr 30, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion utility.class.php
    Original file line number Diff line number Diff line change
    @@ -97,7 +97,7 @@ public function trimArray(array $array, $charlist = null) {
    $newArray = array();
    foreach ($array as $k => $v) {
    if (is_array($v)) {
    $array[$k][$v] = $this->trimArray($v);
    $array[$k][$v] = $this->trimArray($v, $charlist);
    } else {
    $val = $this->trimString($v, $charlist);
    if (empty($v)) {
  5. goldsky created this gist Apr 30, 2012.
    138 changes: 138 additions & 0 deletions utility.class.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,138 @@
    <?php

    /**
    * @license The include classes have their own licenses
    * @copyright (c) 2012 by goldsky <goldsky@fastmail.fm>
    */
    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 <aidan@php.net>
    * @author Peter Waller <iridum@php.net>
    * @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(array $array, $charlist = null) {
    $newArray = array();
    foreach ($array as $k => $v) {
    if (is_array($v)) {
    $array[$k][$v] = $this->trimArray($v);
    } else {
    $val = $this->trimString($v, $charlist);
    if (empty($v)) {
    continue;
    }
    $newArray[$k] = $val;
    }
    }
    sort($newArray);

    return $newArray;
    }

    /**
    * 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) {
    $string = htmlentities($string);
    // blame TinyMCE!
    $string = preg_replace('/(&Acirc;|&nbsp;)+/i', '', $string);
    if ($charlist === null) {
    $string = trim($string);
    } else {
    $string = trim($string, $charlist);
    }

    if (empty($string)) {
    return FALSE;
    }

    return $string;
    }

    }