1.1 Dissecting Strings


You want to access or change a portion of a string, but not the entire string.

Technique

Use PHP's built-in substr() and substr_replace() functions, which enable you to read and write parts of the string:

 <?php $sub_str = substr ($str, $initial_pos); $sub_str = substr ($str, $initial_pos, $str_len); $new_str = substr_replace ($str, $replacement, $initial_pos); $new_str = substr_replace ($str, $replacement, $initial_pos, $str_len); ?> 

Comments

As said before, PHP treats strings as a basic data type instead of an array of bytes. Therefore, it is possible for you to use a function such as substr() or substr_replace() to access and modify individual characters or portions of strings.

The first argument to substr() is the string on which you want to operate . The second argument to substr() specifies the beginning index of the substring you want to access. If the second argument is positive, the substring will start at that character counting from the beginning of the string. If the second argument is negative, the substring will start at that character counting from the end of the string. A zero value for the second argument means that you are counting from the beginning of the string (with strings, counting always begins at zero ”not one).

The third argument specifies the length of the substring. If the third argument is given and is positive, the substring will end that many characters from its start. If the third argument is negative, the string returned will end that many characters from the end of the string. If the third argument is not supplied, it's assumed that you want to get everything from the start argument to the end of the string.

 <?php $str = 'The quick brown fox jumped over the lazy old dog'; $fox = substr ($str, 16, 3); // fox $fox = substr ($str, -32, 3); // Also fox $middle = substr ($str, 20, 11); // jumped over $end = substr ($str, -13); // lazy old dog $end_char = substr ($str, -1); // g $last_word = substr ($str, -3); // dog ?> 

But what if you want to read the string up to the first occurrence of a character? Simple. The strpos() function gives the position of the first character in a string that matches the character you specify:

 <?php $email = 'sterling@php.net'; $user_name = substr ($email, 0, strpos ($email, '@')); // $user_name contains sterling ?> 

You can also match portions of strings by using the substr() function with ereg() (or any of the regular expression functions, for that matter):

 <?php if (ereg ("yourpattern", substr ($string, -15))) {     print "The last 15 characters of string match the pattern"; } ?> 

If you want to read fixed-length records, I recommend that you use PHP's unpack() function (based on the Perl unpack() ), which is not only faster as far as CPU time is concerned , but is also quicker in terms of programming time:

 <?php $recs = unpack ("A20firstname/A20lastname/A40address/A2state/A5zip",                 $str); // $recs is now an associative array containing elements // firstname, lastname, address, state and zip ?> 

The unpack() function is used to extract binary data from a string and place it into an associative array. unpack() happens to be very useful for reading fixed-length records as well.

You're probably saying, "That's all well and good. unpack() seems like a useful function, but it looks so cryptic!" That's true, but it's worth the effort to learn how to use unpack() . Consider this simple example:

 <?php // read five characters and assign to $rec["zipcode"]; $rec = unpack("A5zipcode", $str); // Same thing with substring $rec["zipcode"] = substr ($str, 0, 5); ?> 

Let's look at the first argument of unpack() : "A5zipcode" . The "A5" specifies that the function is to read a five-character (the 5 ), space- padded string (the A ). So, if the A5 reads a five-character, space-padded string, what is the "zipcode" on the end for? Unlike Perl, PHP's unpack() function returns an associative array. Therefore, the zipcode part of the argument tells the interpreter that the five-character, space-padded string will be referenced by "zipcode" in the associative array.

We have just analyzed a simple unpack() example, but what about the original example I gave you? It might have looked a lot harder, but it's not! Conceptually, the original example is no harder than the simple example; it is just a bunch of format codes separated by a "/" . So, in the original unpack() example, we read 20 bytes and assign them to $recs["firstname"] , and then read 20 more characters and assign them to $recs["lastname"] . Then we read 40 more characters and assign them to $recs["address"] , read 2 more characters and assign them to $recs["state"] , and finally read 5 more characters and assign them to $recs["zip"] .

unpack() is really useful for reading not just space-padded strings, but many other things (as more technical people like to refer to them). Here is a list of all the different format codes that you can use with unpack() :

a NUL-padded string
A Space-padded string
h Hex string; low nibble first
H Hex string; high nibble first
c Signed char
C Unsigned char
s Signed short (always 16 bit, machine byte order)
S Unsigned short (always 16 bit, machine byte order)
n Unsigned short (always 16 bit, big-endian byte order)
v Unsigned short (always 16 bit, little-endian byte order)
i Signed integer ( machine-dependent size and byte order)
I Unsigned integer (machine-dependent size and byte order)
l Signed long (always 32 bit, machine byte order)
L Unsigned long (always 32 bit, machine byte order)
N Unsigned long (always 32 bit, big-endian byte order)
V Unsigned long (always 32 bit, little-endian byte order)
f Float (machine-dependent size and representation)
d Double (machine-dependent size and representation)
x NUL byte; you can use this to skip ahead characters (for example: get five, skip five, get five)
X Back up one byte; you can use this to skip characters from right to left (for example, skip back five characters)
@ NUL-fill to absolute position

And, finally, some examples

 <?php $str = "Gumbi and PDOC are smelly"; $names = unpack ("A5name1/x5/A4name2", $str); // read 5, skip 5, read 4 // $names['name1'] is Gumbi and $names['name2'] is PDOC $str2 = 'A box without hinges, key, or lid, Yet golden treasure inside is hid'; // J.R.R. Tolkien, The Hobbit $results = unpack ("A1pronoun/x5/A7adjective/X11/A3noun", $str2); // read 1, skip 5, read 7, go back 11, read 3 //(the answer is eggs) ?> 


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