Advertisement
Guest User

Untitled

a guest
Oct 18th, 2010
336
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3. * Multibyte safe version of trim()
  4. * Always strips whitespace characters (those equal to \s)
  5. *
  6. * @author Peter Johnson
  7. * @email phpnet@rcpt.at
  8. * @param $string The string to trim
  9. * @param $chars Optional list of chars to remove from the string ( as per trim() )
  10. * @param $chars_array Optional array of preg_quote'd chars to be removed
  11. * @return string
  12. */
  13. public static function mb_trim( $string, $chars = "", $chars_array = array() )
  14. {
  15.     for( $x=0; $x<iconv_strlen( $chars ); $x++ ) $chars_array[] = preg_quote( iconv_substr( $chars, $x, 1 ) );
  16.     $encoded_char_list = implode( "|", array_merge( array( "\s","\t","\n","\r", "\0", "\x0B" ), $chars_array ) );
  17.  
  18.     $string = mb_ereg_replace( "^($encoded_char_list)*", "", $string );
  19.     $string = mb_ereg_replace( "($encoded_char_list)*$", "", $string );
  20.     return $string;
  21. }
  22. ?>
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement