Strings and Character Positions

December 3rd, 2009

A sequence of characters compose a string. PHP comes equipped with an arsenal of string related functions. This posting will cover three string functions: strlen, strpos and strrpos.

Strings are enclosed in a pair of either single or double quotation marks. Characters are taken at face value when enclosed in single doubles. Additional processing occurs when characters are enclosed in double quotation marks; variable substitutions are made and escaped characters (e.g. \n) are translated into character codes.

In the example below, $firstSaying and $secondSaying have matching string values. The variable $person is automatically evaluated to ‘day’ because the value of $firstString is enclosed in double quotes.

<?php

$duration = 'day';

$firstSaying = "An apple a $duration";
$secondSaying = 'An apple a day';

?>

Internally, the value of $secondSaying is:

Character A n   a p p l e   a   d a y
Position 0 1 2 3 4 5 6 7 8 9 10 11 12 13

The table above the shows the position of each character within the string. Some programming languages use a starting position of one (Visual Basic) while the majority use a starting position of zero.

To retrieve a single character from a string, place the character position enclosed in brackets [ ] immediately after the string variable. For example, the following retrieves and displays 13th character in $secondString.

<?php

$secondSaying = 'An apple a day';

echo($secondSaying[13]);

?>

This program outputs the character y.

PHP is lenient when attempting at get characters that falls outside boundaries of a string (e.g. retrieving characters using a negative position or beyond the length of the string. It always returns an empty string “”. Bear in mind that trying this in programming languages may get you into trouble.

String Length

The function strlen accepts one argument, a string, and returns the length of that string. Since the first character begins at position zero, the string length is one greater than last character position in the string.

<?php

$secondSaying = 'An apple a day';
//Retrieve the length of the string
$secondSayingLength = strlen($secondSaying);

echo("The length of string '$secondSaying' is $secondSayingLength.\n");

?>

Output: The length of string 'An apple a day' is 14.

Calling strlen on a null value returns a length of zero.

Searching within Strings

To locate the starting position of text with a string, use the function strpos. Standard calls to strpos accept two arguments, the first parameter being the string to search (the subject or the haystack) and second the text to find (the needle). The text can be a single character or a string consisting of multiple characters. The function will begin at the left hand side of the string and search right for the string. If the search text cannot be located, a boolean value false is returned.

Note: Be careful when checking if the search text is found. The starting position of 0 has the same value as the boolean false. Use the identical === binary operator or the not identical operator !== to make this differentiation.

<?php

$secondSaying = 'An apple a day';
$searchText = 'a';
//Find the position of 'a'
$positionOfA = strpos($secondSaying, $searchText);

if ($positionOfA !== false) {
        echo("The first occurrence of '$searchText' is at position $positionOfA.\n");
}
else {
        echo("Could not locate $searchText\n");
}

?>

The source code above retrieves the first occurrence of a lowercase ‘a’.

Character A n   a p p l e   a   d a y
Position 0 1 2 3 4 5 6 7 8 9 10 11 12 13

Output: The first occurrence of 'a' is at position 3.

But the first occurrence of the letter a is at position zero.

Well, strpos is case sensitive. It locates the first lowercase ‘a’; an uppercase ‘A’ isn’t a lowercase one. To locate the first occurrence of of ‘a’ regardless of its case use stripos. In the example above it returns the integer zero.

Case insensitive string functions ignore the case of characters when performing operations. These functions begin with stri where their case sensitive counterparts begin with str. For example, stricmp is case insensitive and strcmp is case sensitive.

The function strpos accepts an optional parameter, the position within the string (offset) to conduct a search. To find the second ‘a’ within the string, I will begin searching one character after the first ‘a’ at $positionOfA + 1.

Character A n   a p p l e   a   d a y
Position 0 1 2 3 4 5 6 7 8 9 10 11 12 13

I cannot assume that there will always be text following the first ‘a’ to search on. PHP doesn’t care if you search outside the boundary of the string, but in this example, I will keep my search within the string by using strlen.

<?php

$secondSaying = 'An apple a day';
$secondSayingLength = strlen($secondSaying);
$searchText = 'a';

//Find the position of 'a'
$positionOfA = strpos($secondSaying, $searchText);
$positionOfSecondA = false;

if ($positionOfA !== false) {
        // Find the position of the second 'a'
        if ($secondSayingLength > ($positionOfA + 1)) {
                $positionOfSecondA = strpos($secondSaying, $searchText, $positionOfA + 1);
        }
}

if ($positionOfSecondA !== false) {
        echo("The second occurrence of '$searchText' is at position $positionOfSecondA.\n");
}
else {
        echo("Could not locate second occurrence of '$searchText'.\n");
}

?>

Output: The second occurrence of 'a' is at position 9.

To find the last occurrence ‘a’, use the function strrpos. This searches for a string in reverse, hence the extra ‘r’ in the function name. It accepts the same arguments as the forward version of the search.

For those living in the dark ages of PHP 4 (waves hand), strrpos only searches on single characters. If I wanted to find the last occurrence of the word apple and pass the string ‘apple’ as an argument, strrpos would search for the last occurrence of the letter ‘a’. This will not give us the correct result in PHP 4.

Here’s an example of finding the last occurrence of the letter ‘a’ in PHP 5.

<?php

$secondSaying = 'An apple a day';
$searchText = 'a';

//Find the last position of 'a'
$positionOfA = strrpos($secondSaying, $searchText);

if ($positionOfA !== false) {
        echo("The last occurrence of '$searchText' is at position $positionOfA.\n");
}
else {
        echo("Could not locate the last occurrence of '$searchText'.\n");
}

?>

Output: The last occurrence of 'a' is at position 12.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>