In this section we will cover:
Comparisons
Basic search and replace
Regular Expressions
String functions and formatting
Accessing arrays
Single- and multidimensional arrays
Array iteration
Array sorting
Array functions and manipulation
Serialization
- String Comparison
- String comparison is mostly trivial, but can sometimes be tricky. The equivalence operator should be used when you know that you are comparing two strings—or when you don’t care about cases like this:
- • “123test” == 123 == TRUE!
- The identity operator should be otherwise used every time you know that you want to compare two strings without letting PHP juggle types.
- • PHP also provides function-based comparison:
strcmp()
strcasecmp()
strncmp() and strncasecmp() - Basic String Searching
- strstr() (aliased into strchr()) determines whether a substring exists within a string:
strstr (“PHP is a language”, “PHP”) == true
stristr() provides a case-insensitive search - strpos() will return the location of a substring
inside a string, optionally starting from a given position:
strpos ($haystack, $needle, $pos)
Beware of zero return values!
There is no stripos() in PHP 4! - Reverse search is done with strrchr() / strrpos()
- Counting Strings
- The length of a string is determined with strlen()
Do not use count()! - You can count words inside a string using
str_word_count():
str_word_count ($str, $n);
$n == 1 — Returns array with words in order
$n == 2 — Returns array with words and positions
substr_count() can be used to count the number
of occurrences of a given substring:
substr_count (“phpphpPHP”, “php”) == 2 - Formatting Strings
- Most of the time, strings can be formatted using a combination of concatenations
In some cases, however, it is necessary to use special functions of the printf() family
printf() — outputs formatted strings to STDOUT
printf (“%d”, 10);
sprintf() — returns the formatted string
$a = sprintf (“%d”, 10);
fprintf() — outputs formatted strings to a file descriptor
fprintf ($f, “%d”, 10);
vprintf(), vsprintf() — take input from array
vprintf (“%d”, array (10));
$a = vsprintf (“%d”, array (10)); - Formatting Strings
-
% - a literal percent character.
b – integer presented as a binary number
c – integer (ASCII value)
d – integer (signed decimal number)
e – number in scientific notation (Ex. 1.2e+2)
u integer (unsigned decimal number) –
f – float as a floating-point number.
o – integer (octal number).
s – string
x – hexadecimal number (lowercase letters).
X – hexadecimal number (uppercase letters).
- Accessing Strings as Arrays
- You can access individual characters of a string as if it were an array
$s = “12345”;
echo $s[1]; // Outputs 2
echo $s{1}; // Outputs 2
This works for both reading and writing. Remember that you cannot use count() to determine the number of characters in a string! - Extracting and Replacing
- Substrings can be extracted using the substr() function:
echo substr (“Marco”, 2, 1); // Outputs r
echo substr (“Marco”, -1); // Outputs o
echo substr (“Marco”, 1, -1); // Outputs arc
Substrings can be replaced using substr_replace():
substr_replace (‘Marco’, ‘acr’, 1, -1) == “Macro”
The sscanf() function can be used to extract tokens formatted à la printf() from a string:
sscanf(“ftp://127.0.0.1”, "%3c://%d.%d.%d.%d:%d");
Returns array (‘ftp’, ‘127’, ‘0’, ‘0’, ‘1’); - Multiple Replacements
- str_replace() replaces instances of a substring with another:
str_replace (“.net”, “arch”, “php.net”) == “phparch”
You can perform multiple replacements by passing arrays to str_replace():
str_replace(array('apples', 'applesauce', 'apple'),
array('oranges', 'orange-juice', 'cookie'),
“apple apples applesauce”)
Returns “cookie oranges orangesauce” - PCRE — Perl Regular Expressions
- Perl Regular Expressions (PCRE) make it possible to search (and replace) variable patterns inside a string
PCRE is usually fast and simple to understand, but it can also be complicated or slow (or both)
Regular expressions are matched using the preg_match() function:
preg_match ($pcre, $search, &$results)
preg_match_all ($pcre, $search, &$results)
Search-and-replace is performed using preg_replace():
preg_replace ($pcre, $replace, $search) - PCRE — Meta Characters
- Meta characters are used inside a regex to represents a series of characters:
\d — digits 0 –9
\D — not a digit
\w — alphanumeric character or underscor
\W — opposite of \w
\s — any whitespace (space, tab, newline)
\S — any non-whitespace character
. — any character except for a newline
• Meta characters only match one character at a time (unless an operator is used to change this behaviour) - PCRE — Operators / Expressions
- PCRE operators indicate repetition:
? — 0 or 1 time
* — 0 or more times
+ — 1 or more times
{,n} — at more n times
{m,} — m or more times
{m,n} — at least m and no more than n times
Parentheses are used to group patterns
(abc)+ — means “abc” one more times
Square brackets indicate character classes
[a-z] means “any character between a and z
The caret negates a class: [^a-z] is the opposite of the expression above - PCRE — An example
- Here’s an example of a PCRE:
$string = ‘123 abc’;
preg_match (‘/\d+\s\[a-z]+/’, $string);preg_match (‘/\w\s\s/’, $string);
preg_match (‘\d{3}\s[a-z]{3}’/, $string);
- PCRE — Another Example
- Here’s an example of how to retrieve data from a regex:
$email = ‘marcot@tabini.ca”;
preg_match (‘/(\w+)@(\w+)\.(\w+)/’);Will return array (‘example@kiran.hota’, ‘example’,
‘kiran’, ‘hota’) - String Splitting and Tokenization
- The explode() function can be used to break up a string into an array using a common delimiter:
explode (‘.’, ‘www.phparch.com’);
Will return array (‘www’, ‘phparch’, ‘com’);
The preg_split() function does the same thing, but using a regex instead of a fixed delimiter:
explode (‘[@.]’, ‘marcot@tabini’ca’);
Will return array (‘marcot’, ‘tabini’, ‘ca’); - Word Wrapping
- The wordwrap() function can be used to break a string using a specific delimiter at a given length
wordwrap ($string, $length, $delimiter, $break);
If the $break parameter evaluates to TRUE, the break occurs at the specified position, even if it occurs in the middle of a word - Arrays
- Arrays are created in a number of ways:
Explicitly by calling the array() function
• array (1, 2, 3, 4);
• array (1 => 1, 2, 3, 5 => “test”);
• array (“2” => 10, “a” => 100, 30);
By initializing a variable using the array operator:
• $x[] = 10;
• $x[-1] = 10;
• $x[‘a’] = 10;
• The count() function is used to determine the number of elements in an array
Executing count() against any other data type (including objects), it will return 1 (or 0 for NULL) - Array Contents
- Array can contain any data type supported by PHP, including objects and other arrays
• Data can be accessed using the array operator
$x = $array[10];
• Multiple elements can be extracted using the list function:
$array = (1, 2, 3);
list ($v1, $v2, $v3) = $array - Array Iteration
- It’s possible to iterate through arrays in a number of ways. Typically:
for ($i = 0; $i < count ($array); $i++) // WRONG!
$cnt = count ($array)
for ($i = 0; $i < $cnt; $i++)
Storing the invariant array count in a separate variable improves performance
foreach ($array as $k => $v)
$k and $v are assigned by value—therefore, changing them won’t affect the values in the array. However, you can change the array directly using $k:
$array[$k] = $newValue; - Array Iteration
- You can also iterate through an array using the internal array pointer:
$a = array(1,2,3);while (list($k, $v) = each($a)) {
echo "{$k} => {$v} ";
if ($k % 2) { // add entry if key is odd
$a[] = $k + $v;
}
} // 0 => 1 1 => 2 2 => 3 3 => 3 4 => 6
With this approach, operations take place directly on the array
Finally, you can use array_callback() to iterate through an array using a user-supplied function - Array Keys and Values
- You can check if an element exists in one of two ways:
array_key_exists ($array, $key); // Better, but slower
isset ($array[$key]); // Faster, but has pitfalls
$a[1] = null;
echo isset ($a[1]);
You can also check whether a value exists:
in_array ($value, $array)
You can extract all the keys and values from an array using specialized functions:
array_keys ($array);
array_value ($array); - Sorting Arrays
- The sort() and rsort() functions sort an array in-place
sort ($array); — rsort ($array)
Key association is lost—you can use asort() and arsort() to maintain it
A more “natural” sorting can also be performed:
natsort ($array);
natcasesort ($array);
Sorting by key is also a possibility:
ksort();
krsort(); - Array Functions
- Changing key case:
array_change_key_case ($a, CASE_LOWER)
array_change_key_case ($a, CASE_UPPER)
Randomizing the contents of an array:
shuffle($array)
Extracting a random value:
array_rand ($array, $qty); - Merge, Diff and Sum
- Merging arrays:
array_merge ($a, $b[, ...]);
Later values with the same key overwrite earlier ones
Diff’ing arrays:
array_diff ($a, $b[, ...]);
Returns keys that are not common to all the arrays
Key association is lost—you can use array_diff_assoc()
to maintain it
• Intersecting:
array_intersect ($a, $b[, ...]);
• Calculating arithmetic sum:
array_sum ($array); - Unique Array Values
- The array_unique() function retrieves all the unique array values
array_unique ($array)
Requires traversal of entire array and therefore hampers performance - Arrays as stacks or queue
- The array_push() function pushes a new value at the end of an array
array_push ($array, $value)
Essentially equivalent to $array[] = $value;
The array_pop() retrieves the last value from an array:
$x = array_pop ($array);
This allows you to use arrays as if they were stacks (LIFO)
You can also pull a value from the top of the array, thus implementing a queue (FIFO)
$x = array_shift ($array) - Serializing Arrays
- Like with objects, you can serialize arrays so that they can be conveniently stored outside your script:
$s = serialize ($array);$array = unserialize ($s);
Unserialization will preserve references inside an array, sometimes with odd results
- 1818 reads












