Investigating Strings in PHP

Investigating Strings in PHP
You do not always know everything about the data you are working with. Strings can arrive from many sources, including user input, databases, files, and web pages. Before you begin to work with data from an external source, you often will need to find out more about the data. PHP provides many functions that enable you to acquire information about strings.

آموزش زبان برنامه نویسی PHP برای طراحی سایت اختصاصی یا طراحی اختصاصی سایت

ارایه درگاه پرداخت اینترنتی ePayBank.ir برای سایتهای برنامه نویسی شده اینترنتی آنلاین

تبلیغات نیازمندی خود را میتوانید بصورت آنلاین و ستاره دار در MyCityAd.ir درج اگهی کنید.

A Note About Indexing Strings
We will frequently use the word index in relation to strings. You will have come across this word in the context of arrays, such as in Chapter 8, “Working with Arrays.” In fact, strings and arrays are not as different as you might imagine. You can think of a string as an array of characters, thus you can access the individual characters of a string as if they were elements of an array:

<?php
$test = “phpcoder”;
echo $test[0]; // prints “p”
echo $test[4]; // prints “o”
?>

It is important to remember that when we talk about the position or index of a character within a string, the characters, just like array elements, have a starting index value of 0, not 1.

Finding the Length of a String with strlen()
You can use the built-in strlen() function to determine the length of a string. This function requires a string as its argument and returns an integer representing the number of characters in the string you have passed to it. strlen() might be used to check the length of user input, as in the following fragment, which tests a membership code to ensure that it is exactly four digits long:

<?php
if (strlen($membership) == 4) {
echo “<p>Thank you!</p>”;
} else {
echo “<p>Your membership number must have 4 digits.</p>”;
}
?>

The user is thanked for his input only if the $membership variable holds a string that is exactly four characters long. Otherwise, an error message is presented.

Finding a Substring Within a String with strstr()
You can use the strstr() function to test whether a string exists within another string. This function requires two arguments: the source string and the substring you want to find within it. The function returns false if the substring cannot be found; otherwise, it returns the portion of the source string, beginning with the substring. For the following example, imagine that we want to treat membership codes that contain the string AB differently from those that do not:

<?php
$membership = “pAB7”;
if (strstr($membership, “AB”)) {
echo “<p>Your membership expires soon!</p>”;
} else {
echo “<p>Thank you!</p>”;
}
?>

Because the value of the $membership variable contains the substring AB, the strstr() function returns the string AB7. The function resolves to true when tested, so we print the appropriate message, “Your membership expires soon!”. But what happens if we search for “pab7”? Because strstr() is case sensitive, AB will not be found. The if statement’s original test will fail, and the default message will be printed to the browser (“Thank you!”). If we want to search for either AB or ab within the string, we must use strstr() in place of substr(); the function is used in exactly the same way, but its search is not case sensitive.

Finding the Position of a Substring with strpos()
The strpos() function tells you whether a string exists within a larger string as well as where it is found. The strpos() function requires two arguments: the source string and the substring you are seeking. The function also accepts an optional third argument, an integer representing the index from which you want to start searching. If the substring does not exist, strpos() returns false; otherwise, it returns the index at which the substring begins. The following fragment uses strpos() to ensure that a string begins with the string mz:

<?php
$membership = “mz00xyz”;
if (strpos($membership, “mz”) === 0) {
echo “Hello mz!”;
}
?>

Notice the trick we had to play to get expected results. Although the strpos() function finds mz in our string, it finds it at the first elementthe 0 position. Returning zero will resolve to false in our if condition test. To work around this, we use the equivalence operator ===, which returns true if the left- and right-hand operands are equivalent and of the same type, as they are in this case.

This is just one of several variations on string-related functions meant to find needles in haystacks. Visiting the PHP Manual page for this function provides links to many other related functions.

Extracting Part of a String with substr()
The substr() function returns a string based on the start index and length of the characters you are looking for. This function requires two arguments: a source string and the starting index. Using these arguments, the function returns all the characters from the starting index to the end of the string you are searching. You can also (optionally) provide a third argumentan integer representing the length of the string you want returned. If this third argument is present, substr() returns only that number of characters, from the start index onward.

<?php
$test = “phpcoder”;
echo substr($test,3).”<br/>”; // prints “coder”
echo substr($test,3,2).”<br/>”; // prints “co”
?>

If you pass substr() a negative number as its second (starting index) argument, it will count from the end rather than the beginning of the string. The following fragment writes a specific message to people who have submitted an email address ending in .fr:

<?php
$test = “info@epaybank.ir”;
if ($test = substr($test, -3) == “.fr”) {
echo “<p>Bonjour! Nous avons des prix spéciaux de vous.</p>”;
} else {
echo “<p>Welcome to our store.</p>”;
}
?>

Tokenizing a String with strtok()
You can parse a string word by word using the strtok() function. This function requires two arguments: the string to be tokenized and the delimiters by which to split the string. The delimiter string can include as many characters as you want, and the function will return the first token found. After strtok() has been called for the first time, the source string will be cachedfor subsequent calls, you should only pass the delimiter string to the strtok() function. The function will return the next found token every time it is called, returning false when the end of the string is reached. The strtok() function will usually be called repeatedly, within a loop. Listing 10.3 uses strtok() to tokenize a URL, splitting the host and path from the query string, and further dividing the name/value pairs of the query string.

Listing 10.3. Dividing a String into Tokens with strtok()
1: <?php
2: $test = “http://www.google.com/search?”;
3: $test .= “hl=en&ie=UTF-8&q=php+development+books&btnG=Google+Search”;
4: $delims = “?&”;
5: $word = strtok($test, $delims);
6: while (is_string($word)) {
7: if ($word) {
8: echo $word.”<br/>”;
9: }
10: $word = strtok($delims);
11: }
12: ?>

Put these lines into a text file called teststrtotok.php and place this file in your web server document root. When you access this script through your web browser, it should look like Figure 10.4.

Figure 10.4. Output of teststrtotok.php, a tokenized string.

The strtok() function is something of a blunt instrument, and a few tricks are required to work with it. We first store the delimiters we want to work with in a variable, $delims, on line 4. We call strtok() on line 5, passing it the URL we want to tokenize and the $delims string. We store the first result in $word. Within the conditional expression of the while loop on line 6, we test that $word is a string. If it isn’t, we know that the end of the string has been reached and no further action is required.

We are testing the return type because a string containing two delimiters in a row would cause strtok() to return an empty string when it reaches the first of these delimiters. So, a more conventional test such as

while ($word) {
$word = strtok($delims);
}

would fail if $word is an empty string, even if the end of the source string has not yet been reached.

Having established that $word contains a string, we can go on to work with it. If $word does not contain an empty string, we print it to the browser on line 8. We must then call strtok() again on line 10 to repopulate the $word variable for the next test. Notice that we don’t pass the source string to strtok() a second time if we did this, the first word of the source string would be returned once again, and we would find ourselves in an infinite loop.