Advertisement
Danack

Shitty functional include

Jul 6th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.10 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Intahwebz\MBExtra{
  4.  
  5.  
  6. class Functions{
  7.     public static function load(){
  8.     }
  9. }
  10.  
  11. }
  12.  
  13.  
  14.  
  15. namespace {
  16.  
  17. // mb_string - the missing functions.
  18. // all these functions are meant to be the mb_ equivalent of their non-multi-byte-safe versions.
  19. // If they are not, then they have a bug. Please consider fixing and sending a pull request.
  20.  
  21. function mb_ucfirst($string){
  22.     return mb_convert_case($string, MB_CASE_UPPER);
  23. }
  24.  
  25. function mb_ucwords($string){
  26.     return mb_convert_case($string, MB_CASE_TITLE);
  27. }
  28.  
  29. function mb_lcfirst($str) {
  30.     return mb_strtolower(mb_substr($str,0,1)).mb_substr($str,1);
  31. }
  32.  
  33.  
  34. function mb_str_split($str, $l = 0) {
  35.     if ($l > 0) {
  36.         $ret = array();
  37.         $len = mb_strlen($str, "UTF-8");
  38.         for ($i = 0; $i < $len; $i += $l) {
  39.             $ret[] = mb_substr($str, $i, $l, "UTF-8");
  40.         }
  41.         return $ret;
  42.     }
  43.     return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
  44. }
  45.  
  46. function mb_strcasecmp($str1, $str2, $encoding = null) {
  47.     if (null === $encoding) {
  48.         $encoding = mb_internal_encoding();
  49.     }
  50.  
  51.     return strcmp(mb_strtoupper($str1, $encoding), mb_strtoupper($str2, $encoding));
  52. }
  53.  
  54. function mb_strrev($str){
  55.     preg_match_all('/./us', $str, $ar);
  56.     return join('',array_reverse($ar[0]));
  57. }
  58.  
  59. //Taken from http://www.php.net/manual/en/function.substr-replace.php#90146
  60. function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = null)
  61. {
  62.     $string_length = (is_null($encoding) === true) ? mb_strlen($string) : mb_strlen($string, $encoding);
  63.  
  64.     if ($start < 0)
  65.     {
  66.         $start = max(0, $string_length + $start);
  67.     }
  68.     else if ($start > $string_length)
  69.     {
  70.         $start = $string_length;
  71.     }
  72.  
  73.     if ($length < 0)
  74.     {
  75.         $length = max(0, $string_length - $start + $length);
  76.     }
  77.     else if ((is_null($length) === true) || ($length > $string_length))
  78.     {
  79.         $length = $string_length;
  80.     }
  81.  
  82.     if (($start + $length) > $string_length)
  83.     {
  84.         $length = $string_length - $start;
  85.     }
  86.  
  87.     if (is_null($encoding) === true)
  88.     {
  89.         return mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length);
  90.     }
  91.  
  92.     return mb_substr($string, 0, $start, $encoding) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length, $encoding);
  93. }
  94.  
  95. function mb_wordwrap($string, $width=75, $break="\n", $cut=false)
  96. {
  97.     $width = intval($width); //Used in match - don't trust input
  98.  
  99.     if($cut) {
  100.         // Match anything 1 to $width chars long followed by whitespace or EOS,
  101.         // otherwise match anything $width chars long
  102.         $search = '/(.{1,'.$width.'})(?:\s|$)|(.{'.$width.'})/uS';
  103.         $replace = '$1$2'.$break;
  104.     } else {
  105.         // Anchor the beginning of the pattern with a lookahead
  106.         // to avoid crazy backtracking when words are longer than $width
  107.         $pattern = '/(?=\s)(.{1,'.$width.'})(?:\s|$)/uS';
  108.         $replace = '$1'.$break;
  109.     }
  110.     return preg_replace($search, $replace, $string);
  111. }
  112.  
  113.  
  114.  
  115. /**
  116.  * Replace all occurrences of the search string with the replacement string.
  117.  *
  118.  * @author Sean Murphy <sean@iamseanmurphy.com>
  119.  * @copyright Copyright 2012 Sean Murphy. All rights reserved.
  120.  * @license http://creativecommons.org/publicdomain/zero/1.0/
  121.  * @link http://php.net/manual/function.str-replace.php
  122.  *
  123.  * @param mixed $search
  124.  * @param mixed $replace
  125.  * @param mixed $subject
  126.  * @param int $count
  127.  * @return mixed
  128.  */
  129. function mb_str_replace($search, $replace, $subject, &$count = 0) {
  130.     if (!is_array($subject)) {
  131.         // Normalize $search and $replace so they are both arrays of the same length
  132.         $searches = is_array($search) ? array_values($search) : array($search);
  133.         $replacements = is_array($replace) ? array_values($replace) : array($replace);
  134.         $replacements = array_pad($replacements, count($searches), '');
  135.  
  136.         foreach ($searches as $key => $search) {
  137.             $parts = mb_split(preg_quote($search), $subject);
  138.             $count += count($parts) - 1;
  139.             $subject = implode($replacements[$key], $parts);
  140.         }
  141.     } else {
  142.         // Call mb_str_replace for each subject in array, recursively
  143.         foreach ($subject as $key => $value) {
  144.             $subject[$key] = mb_str_replace($search, $replace, $value, $count);
  145.         }
  146.     }
  147.  
  148.     return $subject;
  149. }
  150.  
  151.  
  152. }
  153.  
  154. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement