©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
(PHP 4 >= 4.3.0, PHP 5)
str_shuffle — 随机打乱一个字符串
$str
)str_shuffle() 函数打乱一个字符串,使用任何一种可能的排序方案。
str
输入字符串。
返回打乱后的字符串。
Example #1 str_shuffle() 范例
<?php
$str = 'abcdef' ;
$shuffled = str_shuffle ( $str );
// 输出类似于: bfdaec
echo $shuffled ;
?>
[#1] ronald [2015-10-04 11:44:07]
This page is missing a very important notice:
Caution
This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using random_int(), random_bytes(), or openssl_random_pseudo_bytes() instead.
[#2] blamoo2 at hotmail dot com [2015-07-15 13:35:18]
This function is affected by srand():
<?php
srand(12345);
echo str_shuffle('Randomize me') . '<br/>'; // "demmiezr aon"
echo str_shuffle('Randomize me') . '<br/>'; // "izadmeo rmen"
srand(12345);
echo str_shuffle('Randomize me') . '<br/>'; // "demmiezr aon" again
?>
[#3] krzysiekpiasecki at gmail dot com [2015-05-23 09:06:01]
function testShuffleString() {
$shuffled = shuffleString("?????");
if (\mb_strlen($shuffled) != 5) {
throw new \UnexpectedValueException("Invalid count of characters");
}
if ($shuffled == "?????") {
throw new \UnexpectedValueException("The same string");
}
foreach (["?", "?", "?", "?", "?"] as $char) {
if (\mb_strpos($shuffled, $char) === false) {
throw new \UnexpectedValueException("Character not found");
}
}
}
function shuffleString($stringValue, $startWith = "") {
$range = \range(0, \mb_strlen($stringValue));
shuffle($range);
foreach($range as $index) {
$startWith .= \mb_substr($stringValue, $index, 1);
}
return $startWith;
};
testShuffleString();
echo shuffleString("Hello"); // > 'elHol' (something like this)
echo shuffleString("World!", "Hello "); // > 'Hello do!lrW' (something like this)
[#4] Anonymous [2014-10-14 17:35:37]
str_shuffle isn't recommendable for passwords. Each character exists only one time).
A function like the following one is better for this.
<?php
function generatePassword($length = 8) {
$possibleChars = "abcdefghijklmnopqrstuvwxyz";
$password = '';
for($i = 0; $i < $length; $i++) {
$rand = rand(0, strlen($possibleChars) - 1);
$password .= substr($possibleChars, $rand, 1);
}
return $password;
}
?>
[#5] qeremy [atta] gmail [dotta] com [2012-02-24 11:58:50]
A proper unicode string shuffle;
<?php
function str_shuffle_unicode($str) {
$tmp = preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
shuffle($tmp);
return join("", $tmp);
}
?>
$str = "?eker y?rim"; // My sweet love
echo str_shuffle($str); // i?eymr?ekr ?
echo str_shuffle_unicode($str); // ?r mreyeik?
[#6] Anonymous [2010-06-21 10:32:26]
Shuffle for all encoding formats
<?php
function unicode_shuffle($string, $chars, $format = 'UTF-8')
{
for($i=0; $i<$chars; $i++)
$rands[$i] = rand(0, mb_strlen($string, $format));
$s = NULL;
foreach($rands as $r)
$s.= mb_substr($string, $r, 1, $format);
return $s;
}
?>
[#7] jojersztajner at OXYGEN dot POLAND [2007-06-16 03:27:30]
Aoccdrnig to rseearch at an Elingsh uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoatnt tihng is that the frist and lsat ltteer is at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit a porbelm. Tihs is bcuseae we do not raed ervey lteter by it slef but the wrod as a wlohe.
Hree's a cdoe taht slerbmcas txet in tihs way:
<?php
function scramble_word($word) {
if (strlen($word) < 2)
return $word;
else
return $word{0} . str_shuffle(substr($word, 1, -1)) . $word{strlen($word) - 1};
}
echo preg_replace('/(\w+)/e', 'scramble_word("\1")', 'A quick brown fox jumped over the lazy dog.');
?>
It may be ufseul if you wnat to cetare an aessblicce CTCPAHA.
[#8] CygnusX1 [2007-02-14 13:17:26]
To cobine functionality and simplicity of the two functions below we can have:
<?php
function generatePasswd($numAlpha=6,$numNonAlpha=2)
{
$listAlpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$listNonAlpha = ',;:!?.$
function mb_str_split($string, $split_length = 1)
{
if ($split_length == 1) {
return preg_split("//u", $string, -1, PREG_SPLIT_NO_EMPTY);
} elseif ($split_length > 1) {
$return_value = [];
$string_length = mb_strlen($string, "UTF-8");
for ($i = 0; $i < $string_length; $i += $split_length) {
$return_value[] = mb_substr($string, $i, $split_length, "UTF-8");
}
return $return_value;
} else {
return false;
}
}
}
?>
[#2] Daniel Rhodes [2015-03-02 20:38:49]
It's mentioned in the Return Values section above ("If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element"), but note that an input of empty string will return array(1) { [0]=> string(0) "" }. Interestingly an input of NULL will also return array(1) { [0]=> string(0) "" }.
Compare this with, say, <?php preg_split('//', $inputString, -1, PREG_SPLIT_NO_EMPTY); ?> which will return array(0) { } for an input of empty string or NULL. I find this to be a bit more intuitive.
Hope this helps.
[#3] kunal dot b at indiaproperty dot com [2014-09-26 10:34:45]
Note that in atleast in PHP 5.5.9 (Zend Engine v2.5.0), str_split with an integer value as an argument may return unpredictable results.
If your number contains leading 0's, the result array is unprdictable as it may contain any number of digits from the argument or (mostly) just a 0.
Here are a list of possible values that might be returned:
-Interger
<?
print_r(str_split(0080450)); // does not work
print_r(str_split(strval(0080450))); // neither this
?>
BUT
<?php
print_r(str_split(80450)); // works fine
print_r(str_split(strval(80450))); // so does this
?>
Floating point numbers have their leading and trailing 0s cut off:
<?php
print_r(str_split(0080450.0010)); // works but.. print_r(str_split(strval(0080450.0010))); // same here..
?>
I'm not sure if this can be considered a bug, since this is due to how type conversion and casting works, so i just posted it here.
I've notced that this is how strval() works. Can anyone shed light into this?..
[#4] robert dot johnson at icap dot com [2014-09-11 14:38:46]
<?php
function str_split_unicode($str, $l = 0)
{
return preg_split('/(.{'.$l.'})/us', $str, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
}
?>
[#5] qeremy [atta] gmail [dotta] com [2013-09-19 20:31:47]
A new version of "str_split_unicode" prev.
<?php
function str_split_unicode($str, $length = 1) {
$tmp = preg_split('~~u', $str, -1, PREG_SPLIT_NO_EMPTY);
if ($length > 1) {
$chunks = array_chunk($tmp, $length);
foreach ($chunks as $i => $chunk) {
$chunks[$i] = join('', (array) $chunk);
}
$tmp = $chunks;
}
return $tmp;
}
?>
$s = '?zg??r Yaz?l?m!'; // Open Source!
print_r(str_split_unicode($s));
print_r(str_split_unicode($s, 3));
Array
(
[0] => ?
[1] => z
[2] => g
[3] => ??
[4] => r
[5] =>
[6] => Y
[7] => a
[8] => z
[9] => ?
[10] => l
[11] => ?
[12] => m
[13] => !
)
Array
(
[0] => ?zg
[1] => ??r
[2] => Yaz
[3] => ?l?
[4] => m!
)
[#6] jakub dot lopuszanski at nasza-klasa dot pl [2013-07-05 13:38:56]
To split UTF-8 into 16-charcater slices you can use:
<?php
preg_split('/(?=(.{16})*$)/u', $text);
?>
You can also use look-behind instead of look-ahead if you want the first (not the last) chunk to be the shortest.
[#7] stlawson AT JoyfulEarthTech DOT com [2013-04-28 18:48:28]
The documentation fails to mention what happens when the string length does not divide evenly with the chunk size. Not sure if the same behavior for all versions of PHP so I offer the following code to determine this for your installation. On mine [version 5.2.17], the last chunk is an array the length of the remaining chars.
<?php
$da_string = "When number of chars does't divide evenly";
$len = strlen($da_string);
$chunk_size = 8;
echo "<p> Length of <span style='font-family:monospace'>$da_string</span>: $len</p>\n";
echo "<p> Chunck size: $chunk_size</p>\n";
$parts = str_split($da_string, $chunk_size);
$html = "<table border='5' cellpadding='3' cellspacing='4'>\n";
foreach ($parts as $idx=>$part)
{
$html .= "<tr>\n";
$html .= "<td style='font-family:monospace'>$part</td>";
$chars = str_split($part);
foreach ($chars as $char)
{
if ($char === ' ')
{
$html .= "<td> </td>";
}
else
{
$html .= "<td>$char</td>";
}
}
$html .= "</tr>\n";
}
$html .= "</table>\n";
echo $html;
?>
[#8] Anonymous [2012-06-08 01:02:37]
A little one-liner that came in handy. I thought i'd share for those who want to split a string into two parts by the given offset.
<?php
function str_osplit($string, $offset){
return isset($string[$offset]) ? array(substr($string, 0, $offset), substr($string, $offset)) : false;
}
$str = "Split a string into two parts at offset";
var_dump(str_osplit($str, 12));
?>
[#9] qeremy [atta] gmail [dotta] com [2012-02-24 12:23:47]
A proper unicode string split;
<?php
function str_split_unicode($str, $l = 0) {
if ($l > 0) {
$ret = array();
$len = mb_strlen($str, "UTF-8");
for ($i = 0; $i < $len; $i += $l) {
$ret[] = mb_substr($str, $i, $l, "UTF-8");
}
return $ret;
}
return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
}
?>
$s = "Il?k s??t"; // Mild milk
print_r(str_split($s, 3));
print_r(str_split_unicode($s, 3));
Array
(
[0] => Il?
[1] => ?k
[2] => s??
[3] => t
)
Array
(
[0] => Il?
[1] => k s
[2] => ??t
)
[#10] anon [2010-06-15 08:53:24]
I needed a function that could split a string from the end with any left over chunk being at the beginning of the array (the beginning of the string).
<?php
function str_rsplit($str, $sz)
{
// splits a string "starting" at the end, so any left over (small chunk) is at the beginning of the array.
if ( !$sz ) { return false; }
if ( $sz > 0 ) { return str_split($str,$sz); } // normal split
$l = strlen($str);
$sz = min(-$sz,$l);
$mod = $l % $sz;
if ( !$mod ) { return str_split($str,$sz); } // even/max-length split
// split
return array_merge(array(substr($str,0,$mod)), str_split(substr($str,$mod),$sz));
}
$str = 'aAbBcCdDeEfFg';
str_split($str,5); // return: {'aAbBc','CdDeE','fFg'}
str_rsplit($str,5); // return: {'aAbBc','CdDeE','fFg'}
str_rsplit($str,-5); // return: {'aAb','BcCdD','eEfFg'}
?>
[#11] seebz.net [2010-05-03 06:27:36]
here an equivalent function for unicode string :
<?php
function uni_strsplit($string, $split_length=1)
{
preg_match_all('`.`u', $string, $arr);
$arr = array_chunk($arr[0], $split_length);
$arr = array_map('implode', $arr);
return $arr;
}
[#12] nemostein at hotmail dot com [2009-08-28 11:51:35]
The manual don't says what is returned when you parse a different type of variable.
This is the example:
<?php
$str1 = "Long"; // More than 1 char
$str2 = "x"; // Only 1 char
$str3 = ""; // Empty String
$str4 = 34; // Integer
$str5 = 3.4; // Float
$str6 = true; // Bool
$str7 = null; // Null
$spl1 = str_split($str1);
$spl2 = str_split($str2);
$spl3 = str_split($str3);
$spl4 = str_split($str4);
$spl5 = str_split($str5);
$spl6 = str_split($str6);
$spl7 = str_split($str7);
echo count($spl1); // 4
echo count($spl2); // 1
echo count($spl3); // 1
echo count($spl4); // 2
echo count($spl5); // 3
echo count($spl6); // 1
echo count($spl7); // 1
print_r($spl1);
print_r($spl2);
print_r($spl3);
print_r($spl4);
print_r($spl5);
print_r($spl6);
print_r($spl7);
?>
[#13] mattias at lsahlin dot se [2009-03-19 06:46:43]
For those it may concern:
We encountered trubble when trying to str_split a UTF-8 encoded string, containing such Swedish letters as ?, ? and ?.
It seems that this function splits according to byte-length and not character length. So if the letter "?" takes 2 bytes, then str_split() will only return the first bite of the character "?".
We ain't 100% sure that this is the case but this was anyhow the result we got. So we used the multi-byte functions instead.
[#14] thax [2008-11-23 18:44:01]
Regarding ricordatis comment on preg_match_all('/./u',...) instead of preg_split('//u',...):
You'll have to use the pattern '/./us' with preg_match_all to get exactly the same behaviour w.r.t. newlines. Don't know if this is still faster, though. Oh, and the expected result is in $array[0].
[#15] rrelmy [2008-08-04 07:20:55]
revised function from tatsudoshi
Fixed some bugs, more php5 style compliant
<?php
if(!function_exists('str_split')) {
function str_split($string,$string_length=1) {
if(strlen($string)>$string_length || !$string_length) {
do {
$c = strlen($string);
$parts[] = substr($string,0,$string_length);
$string = substr($string,$string_length);
} while($string !== false);
} else {
$parts = array($string);
}
return $parts;
}
}
?>
[#16] ricordati at web dot de [2008-08-01 07:21:32]
To split unicode text, preg_match_all('/./u', $text, $array); seems faster for large strings than the use of preg_split('//u', $text); suggested by "edgaras dot janusauskas at gmail dot com" below.
[#17] vec [ at t] g dot com [2008-06-21 08:38:10]
the fastast way (that fits my needs) to replace str_split() in php 4 i found is this:
<?php
if(!function_exists('str_split')) {
function str_split($string, $split_length = 1) {
$array = explode("\r\n", chunk_split($string, $split_length));
array_pop($array);
return $array;
}
}
?>
i also tested the provided functions in the comments..
(the differences are 0.001 to 0.00001 sec)
[#18] saeedco [2008-05-21 03:20:22]
This function supportes utf8
(improvement of function str_split_php4)
i tried this function successfully with these languages
1- Chinese
2- Japanese
3- Arabic
4- Turkish
5- Urdu
6- Russian
7- Persian
<?php
function str_split_php4_utf8($str) {
// place each character of the string into and array
$split=1;
$array = array();
for ( $i=0; $i < strlen( $str ); ){
$value = ord($str[$i]);
if($value > 127){
if($value >= 192 && $value <= 223)
$split=2;
elseif($value >= 224 && $value <= 239)
$split=3;
elseif($value >= 240 && $value <= 247)
$split=4;
}else{
$split=1;
}
$key = NULL;
for ( $j = 0; $j < $split; $j++, $i++ ) {
$key .= $str[$i];
}
array_push( $array, $key );
}
return $array;
}
?>
[#19] lskatz at gmail dot com [2008-05-07 11:42:10]
A good use of str_split is reverse translating an amino acid sequence.
<?php
function reverseTranslate($aaSeq,$ntSeq){
$nt=str_split($ntSeq,3);
$aa=str_split($aaSeq,1);
$gapChar=array('*','-');
$numAa=count($aa);
$ntIndex=0;
$newNtSeq="";
for($i=0;$i<$numAa;$i++){
// if the aa is a gap, then just put on a gap character
if(in_array($aa[$i],$gapChar)){
$newNtSeq.='---';
}
else{
$newNtSeq.=$nt[$ntIndex];
$ntIndex++;
}
}
return $newNtSeq;
}
?>
[#20] kevin at metalaxe dot com [2008-03-14 20:49:09]
Response to "Richard Ayotte 18-Jan-2008 09:27":
Slight tweak to prevent the need to call another preg_replace, there were also some bugs in this that I'm surprised didn't get noticed (causing duplicate replaces between the preg_replace calls) :)
Please feel free to optimize further. I'm not the best with lookahead/behinds yet. I also removed the :upper/lower: and it seemed to speed things up too.
<?php
$test = 'CustomerIDWithSomeOtherJETWords';
preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]/', ' $0', $test));
?>
Shaves off a little time anyway. :)
[#21] Anonymous [2008-01-03 12:04:36]
Version of str_split by rlpvandenberg at hotmail dot com is god-damn inefficient and when $i+$j > strlen($text) [last part of string] throws a lot of notice errors. This should work better:
if(! function_exists('str_split'))
{
function str_split($text, $split = 1)
{
$array = array();
for ($i = 0; $i < strlen($text);)
{
$array[] = substr($text, $i, $split);
$i += $split;
}
return $array;
}
}
[#22] rlpvandenberg at hotmail dot com [2007-12-19 11:22:27]
The previous suggestion is almost correct (and will only working for strlen=1. The working PHP4 function is:
<code>
function str_split($text, $split = 1){
//place each character of the string into and array
$array = array();
for ($i=0; $i < strlen($text); $i++){
$key = "";
for ($j = 0; $j < $split; $j++){
$key .= $text[$i+$j];
}
$i = $i + $j - 1;
array_push($array, $key);
}
return $array;
}
</code>
[#23] mohd at ba7rain dot net [2007-11-29 02:24:41]
this function can perform a reverse str_split. I write it for PHP4 but you can rename It for other versions..
if ( !function_exists('str_split') ) {
function str_split($string,$split_length=1){
$sign = (($split_length<0)?-1:1);
$strlen = strlen($string);
$split_length = abs($split_length);
if ( ($split_length==0) || ($strlen==0) ){
$result = false;
//$result[] = "";
}
elseif ($split_length >= $strlen){
$result[] = $string;
}
else {
$length = $split_length;
for ($i=0; $i<$strlen; $i++){
$i=(($sign<0)?$i+$length:$i);
$result[] = substr($string,$sign*$i,$length);
$i--;
$i=(($sign<0)?$i:$i+$length);
if ( ($i+$split_length) > ($strlen) ){
$length = $strlen-($i+1);
}
else {
$length = $split_length;
}
}
}
return $result;
}
}
[#24] atolia at gmail----- dot com [2007-11-10 13:48:19]
i use this in PHP4
function str_split($str){
return preg_split('//',$str);
}
[#25] magisano at cs dot unibo dot it [2007-11-06 08:14:39]
Even shorter version:
//place each character (or group of) of the
string into and array
function str_split_php4($sText, $iSplit = 1)
{
$iSplit=(integer) $iSplit; // sanity check
if ($iSplit < 1) { return false; }
$aResult = array();
for($i=0, $limit=strlen($sText); $i < $limit; $i+=$iSplit) {
$aResult[]=substr($sText, $i, $iSplit);
}
return $aResult;
}
[#26] dacmeaux at gmail dot com [2007-11-01 13:57:44]
I was looking for a function that would split a string into an array like str_split() and found Razor's function above. Just though that I would simplify the code a little.
<?php
function str_split_php4($text, $split = 1){
//place each character of the string into and array
$array = array();
for($i=0; $i < strlen($text); $i++){
$key = NULL;
for ($j = 0; $j < $split; $j++){
$key .= $text[$i];
}
array_push($array, $key);
}
return $array;
}
?>
Both mine and worksRazor's work well, I just prefer to use less code. I could have written one myself, but I was just being lazy.
[#27] Sam [2007-09-25 01:24:25]
A good way to use this method to convert CamelCase text into nice text would be-
<?php
function FormatCamelCase( $string ) {
$output = "";
foreach( str_split( $string ) as $char ) {
strtoupper( $char ) == $char and $output and $output .= " ";
$output .= $char;
}
return $output;
}
?>
[#28] kjensen at iaff106 dot com [2007-08-11 12:59:42]
Here is what I use. I started with examples here but modified to my own version:
<?php
if (phpversion () < "5"){ // define PHP5 functions if server uses PHP4
function str_split($text, $split = 1)
{
if (!is_string($text)) return false;
if (!is_numeric($split) && $split < 1) return false;
$len = strlen($text);
$array = array();
$s = 0;
$e=$split;
while ($s <$len)
{
$e=($e <$len)?$e:$len;
$array[] = substr($text, $s,$e);
$s = $s+$e;
}
return $array;
}
}
?>
[#29] l0c4lh0st DOT nl AT gmail DOT com [2007-06-12 05:28:27]
how I can conwert
$string
'1, 2, 5, 6, 10, 13, 23'
from ENUM at mySQL to
$array
[0] -> false
[1] -> true
[2] -> true
[3] -> false
[4] -> false
[5] -> true
[6] -> true
[7] -> false
[8] -> false
[9] -> false
[10] -> true
[11] -> false
[12] -> false
[13] -> true
[14] -> false
[15] -> false
...
[23] -> true
<?php
function enum_to_array($psEnum)
{
$aReturn = array();
$aTemp = explode(', ', $psEnum);
for ($i = $aTemp[0]; $i <= $aTemp[count($aTemp)-1]; $i++)
{
$aReturn[$i] = in_array($i, $aTemp);
}
}
?>
[#30] Razor [2007-05-10 12:02:08]
heres my version for php4 and below
<?php
function str_split_php4($text, $split = 1)
{
if (!is_string($text)) return false;
if (!is_numeric($split) && $split < 1) return false;
$len = strlen($text);
$array = array();
$i = 0;
while ($i < $len)
{
$key = NULL;
for ($j = 0; $j < $split; $j += 1)
{
$key .= $text{$i};
$i += 1;
}
$array[] = $key;
}
return $array;
}
?>
[#31] webmaster at nsssa dot ca [2006-10-28 07:45:40]
I noticed in the post below me that his function would return an array with an empty key at the end.
So here is just a little fix for it.
<?php
//Create a string split function for pre PHP5 versions
function str_split($str, $nr) {
//Return an array with 1 less item then the one we have
return array_slice(split("-l-", chunk_split($str, $nr, '-l-')), 0, -1);
}
?>
[#32] fstorm2 at gmail dot com [2006-08-29 02:08:22]
If you use PHP 4 and don't need the split_length parameter, here's the shortest replacement:
<?php
preg_split('#(?<=.)(?=.)#s', $str);
?>
[#33] malmsteenforce at tlen dot pl [2006-07-19 22:52:39]
<?php
//fast & short version od str_split
function string_split($str)
{
$str_array=array();
$len=strlen($str);
for($i=0;$i<$len;$i++) $str_array[]=$str{$i};
return $str_array;
}
//example :
var_dump (string_split("split this"));
?>
[#34] user at mockme dot com [2006-03-25 05:53:50]
found this great example on a php board for those not using php5, as an alternative to the posts below this
<?php
if(!function_exists('str_split')){
function str_split($string,$split_length=1){
$count = strlen($string);
if($split_length < 1){
return false;
} elseif($split_length > $count){
return array($string);
} else {
$num = (int)ceil($count/$split_length);
$ret = array();
for($i=0;$i<$num;$i++){
$ret[] = substr($string,$i*$split_length,$split_length);
}
return $ret;
}
}
}
?>
[#35] heavyraptor [2006-03-10 14:07:10]
I think that the last post by carlosreche at yahoo dot com is too complicated.
It's much easier if you do it like this:
<?php
if (!function_exists("str_split")) {
function str_split($str,$length = 1) {
if ($length < 1) return false;
$strlen = strlen($str);
$ret = array();
for ($i = 0; $i < $strlen; $i += $length) {
$ret[] = substr($str,$i,$length);
}
return $ret;
}
}
?>
I hope it helps for those with PHP <5
[#36] carlosreche at yahoo dot com [2006-02-15 04:23:18]
For those who work with PHP < 5:
<?php
if (!function_exists("str_split")) {
function str_split($string, $length = 1) {
if ($length <= 0) {
trigger_error(__FUNCTION__."(): The the length of each segment must be greater then zero:", E_USER_WARNING);
return false;
}
$splitted = array();
$str_length = strlen($string);
$i = 0;
if ($length == 1) {
while ($str_length--) {
$splitted[$i] = $string[$i++];
}
} else {
$j = $i;
while ($str_length > 0) {
$splitted[$j++] = substr($string, $i, $length);
$str_length -= $length;
$i += $length;
}
}
return $splitted;
}
}
?>
[#37] Hage Yaapa [2006-02-04 06:27:25]
The very handy str_split() was introduced in PHP 5, but a lot of us are still forced to use PHP 4 at our host servers. And I am sure a lot of beginners have looked or are looking for a function to accomplish what str_split() does.
Taking advantge of the fact that strings are 'arrays' I wrote this tiny but useful e-mail cloaker in PHP, which guarantees functionality even if JavaScript is disabled in the client's browser. Watch how I make up for the lack of str_split() in PHP 4.3.10.
<?php
// cloackEmail() accepts a string, the email address to be cloaked
function cloakEmail($email) {
// We create a new array called $arChars, which will contain the individula characters making up the email address. The array is blank for now.
$arChars = array();
// We extract each character from the email 'exploiting' the fact that strings behave like an array: watch the '$email[$i]' bit, and beging to fill up the blank array $arChars
for ($i = 0; $i < strlen($email); $i++) { $arChars[] = $email[$i]; }
// Now we work on the $arChars array: extract each character in the array and print out it's ASCII value prefixed with '&#' to convert it into an HTML entity
foreach ($arChars as $char) { print '&#'.ord($char); }
// The result is an email address in HTML entities which, I hope most email address harvesters can't read.
}
print cloakEmail('someone@nokikon.com');
?>
###### THE CODE ABOVE WITHOUT COMMENTS ######
<?php
function cloakEmail($email) {
$arChars = array();
for ($i = 0; $i < strlen($email); $i++) { $arChars[] = $email[$i]; }
foreach ($arChars as $char) { print '&#'.ord($char); }
}
print cloakEmail('someone@nokikon.com');
?>
In creating this little utility, I demonstrated how the lack of str_split() can be made up in PHP < 5. If you got how it was accomplished, you could write a function to do exactly what str_split() does in PHP 5 and even name it 'str_split()'. :)