]> git.mxchange.org Git - friendica.git/blobdiff - library/phpsec/PHP/Compat/Function/str_split.php
bug #96 move libraries to library - better alignment of like rotator
[friendica.git] / library / phpsec / PHP / Compat / Function / str_split.php
diff --git a/library/phpsec/PHP/Compat/Function/str_split.php b/library/phpsec/PHP/Compat/Function/str_split.php
new file mode 100644 (file)
index 0000000..8e38bdb
--- /dev/null
@@ -0,0 +1,59 @@
+<?php\r
+/**\r
+ * Replace str_split()\r
+ *\r
+ * @category    PHP\r
+ * @package     PHP_Compat\r
+ * @license     LGPL - http://www.gnu.org/licenses/lgpl.html\r
+ * @copyright   2004-2007 Aidan Lister <aidan@php.net>, Arpad Ray <arpad@php.net>\r
+ * @link        http://php.net/function.str_split\r
+ * @author      Aidan Lister <aidan@php.net>\r
+ * @version     $Revision: 1.1 $\r
+ * @since       PHP 5\r
+ * @require     PHP 4.0.0 (user_error)\r
+ */\r
+function php_compat_str_split($string, $split_length = 1)\r
+{\r
+    if (!is_scalar($split_length)) {\r
+        user_error('str_split() expects parameter 2 to be long, ' .\r
+            gettype($split_length) . ' given', E_USER_WARNING);\r
+        return false;\r
+    }\r
+\r
+    $split_length = (int) $split_length;\r
+    if ($split_length < 1) {\r
+        user_error('str_split() The length of each segment must be greater than zero', E_USER_WARNING);\r
+        return false;\r
+    }\r
+    \r
+    // Select split method\r
+    if ($split_length < 65536) {\r
+        // Faster, but only works for less than 2^16\r
+        preg_match_all('/.{1,' . $split_length . '}/s', $string, $matches);\r
+        return $matches[0];\r
+    } else {\r
+        // Required due to preg limitations\r
+        $arr = array();\r
+        $idx = 0;\r
+        $pos = 0;\r
+        $len = strlen($string);\r
+\r
+        while ($len > 0) {\r
+            $blk = ($len < $split_length) ? $len : $split_length;\r
+            $arr[$idx++] = substr($string, $pos, $blk);\r
+            $pos += $blk;\r
+            $len -= $blk;\r
+        }\r
+\r
+        return $arr;\r
+    }\r
+}\r
+\r
+\r
+// Define\r
+if (!function_exists('str_split')) {\r
+    function str_split($string, $split_length = 1)\r
+    {\r
+        return php_compat_str_split($string, $split_length);\r
+    }\r
+}\r