note 61470 added to function.str-split

From: Date: Sat, 04 Feb 2006 14:27:25 +0000
Subject: note 61470 added to function.str-split
Groups: php.notes 
Request: Send a blank email to php-notes+get-102994@lists.php.net to get a copy of this message
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()'. :) ---- Server IP: 203.199.107.94 Probable Submitter: 202.149.60.114 ---- Manual Page -- http://www.php.net/manual/en/function.str-split.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61470 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61470&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61470&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61470&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61470&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61470&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61470&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61470&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61470&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102994) next »