PHP Bible Chapter 8 Strings PHP Bible 2
PHP Bible Chapter 8: Strings ________________________________________________________ PHP Bible, 2 nd Edition 1 Wiley and the book authors, 2002
Summary n n n Creating and manipulating strings Inspecting, comparing, and searching strings Advanced string functions ________________________________________________________ PHP Bible, 2 nd Edition 2 Wiley and the book authors, 2002
Strings in PHP n Strings are sequences of characters that can be treated as a unit Ø Ø n Assigned to variables Given as input to functions Returned from functions Sent as output to appear on your user's web page The simplest way to specify a string in PHP is to enclose it in single or double quotes ________________________________________________________ PHP Bible, 2 nd Edition 3 Wiley and the book authors, 2002
Interpolation with curly braces n In most situations, you can simply include a variable in a doubly -quoted string and the variable's value will be spliced into the string when it is interpreted except when: Ø Ø When text follows the variable name without a space separating them When the expression you want interpolated is not a simple variable If you have the string "I will play $sport 1 ball today", PHP will attempt to find a value for the variable named $sport 1 ball. If the variable name you intended to use is just $sport 1, PHP will replace the text $sport 1 ball with the empty string. To tell PHP that you want to just interpolate the variable name $sport 1, you can write the string as "I will play {$sport 1}ball today" n This construct is also useful for including array elements or object attributes in a string as in "I will play {$sport[1]}ball" ________________________________________________________ n PHP Bible, 2 nd Edition 4 Wiley and the book authors, 2002
Characters and string indices n n In most programming languages, strings are handled as arrays of single characters To access a character at a particular position in a string, you would normally just access the index of the position you wanted (e. g. $stringval[1] would retrieve the 2 nd character in the $stringval variable – note that strings and most arrays in PHP begin indexing at 0) You can access individual characters in strings this way in PHP, although this usage has been deprecated and is not suggested The recommended method, for no apparent reason, is to place the index of the character you wish to retrieve in curly brackets (e. g. $stringval{1}) ________________________________________________________ PHP Bible, 2 nd Edition 5 Wiley and the book authors, 2002
String operators n n PHP offers only one real operator on strings: the dot (. ) or concatenation operator This operator, when placed between two string arguments, produces a new string that is the result of putting the two strings together in sequence $my_two_cents = 'You're a moron'; $third_cent = 'And another thing'; print ($my_two_cents. '. $third_cent. '. . . '); n n In this case, we are not passing multiple string arguments to the print statement, we are passing a single string created by concatenating the 4 strings together Unlike Java, you cannot use the + operator to concatenate 2 strings together. If you try, it will attempt to interpret the strings as numbers and return the addition of the 2 numbers. ________________________________________________________ PHP Bible, 2 nd Edition 6 Wiley and the book authors, 2002
Concatenation and assignment n Just as with arithmetic operators, PHP has a shorthand operator that combines concatenation with assignment (. =) $string_var = 'one plus one = '; $string_var. = 'two'; print ($string_var); Ø n Would print the string "one plus one = two" The string to the right of the concatenation and assignment operator will always be appended to the end (right side) of the string value of the variable on the left side of the operator and assign the new string to the variable on the left side of the operator ________________________________________________________ PHP Bible, 2 nd Edition 7 Wiley and the book authors, 2002
String functions n n n PHP provides a huge variety of functions for the munching and crunching of strings This section presents the basic functions for inspecting, comparing, modifying, and printing strings Note for C programmers: many of the PHP string function names are the same or similar to the corresponding C/C++ function names and perform the same tasks and take the same arguments ________________________________________________________ PHP Bible, 2 nd Edition 8 Wiley and the book authors, 2002
Inspecting strings n int strlen (string str) returns the number of characters in the string Ø Knowing the string's length is particularly useful for situations in which we'd like to loop through a string character by character ________________________________________________________ PHP Bible, 2 nd Edition 9 Wiley and the book authors, 2002
Finding characters and substrings n finds the numerical position of a particular character or string within a string, if it exists int strpos (string haystack, string needle [, int offset]) $test = 'Where is Carmen Santiago? '; $carmen_loc = strpos($test, 'Carmen'); $where_loc = strpos($test, 'Where'); $bob_loc = strpos($test, 'Bob'); $last_i = strrpos($test, 'i'); Ø Would assign the value 9 to $carmen_loc Ø Would assign the value 0 to $where_loc Ø Would assign the value FALSE to $bob_loc Ø Would assign the value 20 to $last_i (strrpos starts looking for the needle at the end of the string) n Note: be careful when using strpos & strrpos in boolean evaluations since if the needle is located at the beginning of the string, the functions would return a value of 0 which is evaluated as FALSE in booleans. Instead, if you are wanting to test if a string appears within another string, use the equality operator === (e. g. if ($strpos($test, 'Where') === FALSE)) ________________________________________________________ PHP Bible, 2 nd Edition 10 Wiley and the book authors, 2002
Comparison n If you are wanting to determine if one string is the same as another, you can use the equality operator (==) if you want to determine if the strings are exactly equal (including case) and you are sure that the values on both sides are strings (e. g. if one of the values is an integer, the other one may be converted to an integer to perform the comparison) int strcmp (string str 1, string str 2) returns < 0 if str 1 is less than str 2; > 0 if str 1 is greater than str 2, and 0 if they are equal int strcasecmp ( string str 1, string str 2) is the same as strcmp except that the comparisons are done case-insensitive ________________________________________________________ PHP Bible, 2 nd Edition 11 Wiley and the book authors, 2002
Searching n n n To find out if one string is contained within another, you can use the strpos function or the strstr function string strstr (string haystack, string needle) returns part of haystack string from the first occurrence of needle to the end of haystack. If needle is not found, returns FALSE Note that one difference between strpos and strstr is that strpos returns an integer based upon the location of the needle, and strstr returns a substring starting at the position of the needle $test = 'Where is Carmen Santiago? '; $carmen_loc = strpos($test, 'Carmen'); Ø Would assign the string "Carmen Santiago? " to $carmen_loc You can perform a case-insensitive search of the haystack using the stristr() function ________________________________________________________ n PHP Bible, 2 nd Edition 12 Wiley and the book authors, 2002
Substring selection n n Many of PHP's string functions have to do with slicing (choosing a portion of a string) and dicing (selectively modifying a string) Ø Most dicing functions do not change the string you started out with, they return the modified string which can then be stored or printed, etc. string substr (string, int start [, int length]) returns the portion of string specified by the start and length parameters Ø If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth. Ø If start is negative, the returned string will start at the start'th character from the end of string Ø If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string). If string is less than start characters long, FALSE will be returned. Ø If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes a position beyond this truncation, an empty string will be returned. ________________________________________________________ PHP Bible, 2 nd Edition 13 Wiley and the book authors, 2002
Substring selection (cont. ) $alphabet_test = 'abcdefghijklmnop'; print('3: '. substr($alphabet_test, 3). '<BR>'); print('-3: '. substr($alphabet_test, -3). '<BR>'); print('3, 5: '. substr($alphabet_test, 3, 5). '<BR>'); print('3, -5: '. substr($alphabet_test, 3, -5). '<BR>'); print('-3, -5: '. substr($alphabet_test, -3, -5). '<BR>'); print('-3, 5: '. substr($alphabet_test, -3, 5). '<BR>'); n Would give us the output 3: defghijklmnop -3: nop 3, 5: defgh 3, -5: defghijk -3, -5: -3, 5: nop ________________________________________________________ PHP Bible, 2 nd Edition 14 Wiley and the book authors, 2002
String cleanup functions n n string rtrim (string str [, string charlist]) returns a string with whitespace stripped from the end of str. The charlist parameter. Allows you to list characters that you want stripped string ltrim (string str [, string charlist]) returns a string with whitespace stripped from the beginning of string trim (string str [, string charlist]) returns a string with whitespace stripped from the beginning and end of str Whitespace characters include Ø Ø Ø n " " (ASCII 32 (0 x 20)), an ordinary space. "t" (ASCII 9 (0 x 09)), a tab. "n" (ASCII 10 (0 x 0 A)), a new line (line feed). "r" (ASCII 13 (0 x 0 D)), a carriage return. "