1.5 Splitting a String by Characters


1.5 Splitting a String by Characters

You need to process a string one character at a time.

Technique

There are two basic ways to process strings character by character. The first way involves the familiar for() loop and the fact that strings are addressable using the [] operator:

 <?php for ($i = 0; $i < strlen ($str); $i++) {     $char = $str[$i];     // manipulate $char here     //  } ?> 

The second way involves the regular expression function preg_split() :

 <?php $chars = preg_split ("//", $str); // $chars is now an array of all the characters in $str ?> 

Comments

PHP's regular expression functions are based on the POSIX standard (http://www.delorie.com/gnu/docs/rx/rx_toc.html). However, that standard does not easily support splitting a string by characters. Therefore, we must rely on PHP's support for Perl-based regular expressions (via the PCRE library). When the preg_split() function is used with the empty regexp "//" , it will split the string into an array of characters.

Let's take the knowledge we've gained from this recipe and recipe 1.4 and create a function that converts a string of characters to their corresponding ASCII codes:

 <?php function str_ord ($str) {     for ($i = 0; $i < strlen ($str); $i++) {         $ascii_values[$i] = ord ($str[$i]);     }     return ($ascii_values); } $str = "Hello World"; $ascii_values = str_ord ($str); print "The third letter of $str, a $str[2],"; print " has an ASCII value of $ascii_values[2]."; ?> 

In the str_ord function, we process the string character-by-character using the method discussed in the beginning of this recipe. Then we use the ord() function to return the ASCII value for the current character in the string, which is added to the end of the $ascii_values array.

There is another way to convert a string to its ASCII values. You can get the ASCII values from a string by using unpack() function and the C format specifier :

 <?php $ascii_values = unpack ("C*char", $str); // $ascii_values is now an associative array with the first character // having the index of "char1" and second character having an index // of "char2", etc. ?> 

The unpack() function returns an associative array. If you want your data in a numerically indexed array, use the array_values() function, which strips away the keys of the array returned by unpack() :

 <?php $ascii_values = array_values (unpack ("C*char", $str)); ?> 

You can also convert the other way around (ASCII to string):

 function ascii_value_to_char ($ascii_values) {     foreach ($ascii_values as $value) {         $str .= chr ($value);     }     return ($str); } ?> 

Here we simply loop through the user -passed array of ASCII values, convert each value back to a character, and append it to the return value ( $str ).



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net