note 61470 deleted from function.str-split by crell
| From: | crell@php.net | Date: | Wed, 29 Jun 2022 17:57:26 +0000 |
| Subject: | note 61470 deleted from function.str-split by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218671@lists.php.net to get a copy of this message | ||
Note Submitter: Hage Yaapa
----
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()'. :)