Handling Strings and Arrays in PHP
- Listing of String Functions
- Using the String Functions
- Formatting Strings
- Converting to and from Strings
- Creating Arrays
- Modifying Arrays
- Removing Array Elements
- Looping Over Arrays
- Listing of the Array Functions
- Sorting Arrays
- Navigating through Arrays
- Imploding and Exploding Arrays
- Extracting Variables from Arrays
- Merging and Splitting Arrays
- Comparing Arrays
- Manipulating the Data in Arrays
- Creating Multidimensional Arrays
- Looping Over Multidimensional Arrays
- Using the Array Operators
- Summary
Two data types merit some special attention—strings and arrays. We've already seen strings at work, including single- and double-quoted strings (recall also that double-quoted strings allow variable interpolation). PHP also comes packed with more string power, and we're going to dig into that in this chapter—tons of functions are built into PHP that work with strings, from sorting strings to searching them, trimming extra spaces off of them, and getting their lengths. We'll get a handle on those functions in this chapter.
Besides strings, we're also going to get a handle on arrays in this chapter. We've seen how to store data in simple variables, but there's more to the story here. Arrays can hold multiple data items, assigning each one a numeric or text index (also called a key). For example, if you want to store some student test scores, you can store them in an array, and then you can access each score in the array via a numeric index. That's great as far as computers are concerned because you can work through all the elements in an array simply by steadily incrementing that index, as you might do with a loop. In that way, you can use your computer to iterate over all the elements in an array in order to print them out or find their average value, for example.
Arrays represent the first time we're associating data items together. Up to this point, we've only worked with simple variables, but working with arrays is fundamental to PHP for such tasks as reading the data that users enter in web pages. We'll get the details on strings and arrays in this chapter, and I'll start with the string functions.
Listing of String Functions
PHP has plenty of built-in string functions. Table 3-1 lists a selection of them.
Table 3-1. The String Functions
Function |
Purpose |
chr |
Returns a specific character, given its ASCII code |
chunk_split |
Splits a string into smaller chunks |
crypt |
Supports one-way string encryption (hashing) |
echo |
Displays one or more strings |
explode |
Splits a string on a substring |
html_entity_decode |
Converts all HTML entities to their applicable characters |
htmlentities |
Converts all applicable characters to HTML entities |
htmlspecialchars |
Converts special characters to HTML entities |
implode |
Joins array elements with a string |
ltrim |
Strips whitespace from the beginning of a string |
number_format |
Formats a number with grouped thousand separators |
ord |
Returns the ASCII value of character |
parse_str |
Parses the string into variables |
|
Displays a string |
printf |
Displays a formatted string |
rtrim |
Strips whitespace from the end of a string |
setlocale |
Sets locale information |
similar_text |
Calculates the similarity between two strings |
sprintf |
Returns a formatted string |
sscanf |
Parses input from a string according to a format |
str_ireplace |
Case-insensitive version of the str_replace function. |
str_pad |
Pads a string with another string |
str_repeat |
Repeats a string |
str_replace |
Replaces all occurrences of the search string with the replacement string |
str_shuffle |
Shuffles a string randomly |
str_split |
Converts a string to an array |
str_word_count |
Returns information about words used in a string |
strcasecmp |
Binary case-insensitive string comparison |
strchr |
Alias of the strstr function |
strcmp |
Binary-safe string comparison |
strip_tags |
Strips HTML and PHP tags from a string |
stripos |
Finds position of first occurrence of a case-insensitive string |
stristr |
Case-insensitive version of the strstr function |
strlen |
Gets a string's length |
strnatcasecmp |
Case-insensitive string comparisons |
strnatcmp |
String comparisons using a "natural order" algorithm |
strncasecmp |
Binary case-insensitive string comparison of the first n characters |
strncmp |
Binary-safe string comparison of the first n characters |
strpos |
Finds position of first occurrence of a string |
strrchr |
Finds the last occurrence of a character in a string |
strrev |
Reverses a string |
strripos |
Finds the position of last occurrence of a case-insensitive string |
strrpos |
Finds the position of last occurrence of a char in a string |
strspn |
Finds the length of initial segment matching mask |
strstr |
Finds the first occurrence of a string |
strtolower |
Converts a string to lowercase |
strtoupper |
Converts a string to uppercase |
strtr |
Translates certain characters |
substr_compare |
Binary-safe (optionally case-insensitive) comparison of two strings from an offset |
substr_count |
Counts the number of substring occurrences |
substr_replace |
Replaces text within part of a string |
substr |
Returns part of a string |
trim |
Strips whitespace from the beginning and end of a string |