Thursday, 26 April 2007
Article: Learn How to Visit Each Character in a String |
| |
|
| |
Ever wondered of a way to get a string in PHP scanned character by character? David Sklar shows you some examples to carry out this procedure.
The PHP 5 solution uses mb_strlen() to find the length and then mb_substr() to grab each character, he says. He gives you a code example to facilitate the process and also adds an example to do it in PHP 6.
He further adds that some rough benchmarks on a 1500 character and 2900 byte string gave him about 61 scans/sec with PHP 5.2.1, when a 'scan' is just moving through the loop above with mb_substr and doing one if() test comparing the char to '>'
Under PHP 6.0.0-dev with unicode.semantics=on, switching from mb_strlen() and mb_substr() to regular strlen() and substr() produces about the same result. The indexing with [] is the same speed as substr(), he initiates.
However, the TextIterator case is much faster, about 450 scans/sec he concludes.
|
| |
|
Read the Post
|
| |
|
|
| |
|
|
| |
|