Книга: Fedora™ Unleashed, 2008 edition

Strings

Strings

Several important functions are used for working with strings, and there are many less frequently used ones for which there is not enough space here. We look at the most important here, ordered by difficulty — easiest first!

The easiest function is strlen(), which takes a string as its parameter and returns the number of characters in there, like this:

<?php
 $ourstring = "  The Quick Brown Box Jumped Over The Lazy Dog  ";
 echo strlen($ourstring);
?>

We will use that same string in subsequent examples to save space. If you execute that script, it outputs 48 because 48 characters are in the string. Note the two spaces on either side of the text, which pad the 44-character phrase up to 48 characters.

We can fix that padding with the trim() function, which takes a string to trim and returns it with all the whitespace removed from either side. This is a commonly used function because all too often you encounter strings that have an extra new line at the end or a space at the beginning. This cleans it up perfectly.

Using trim(), we can turn the 48-character string into a 44-character string (the same thing, without the extra spaces), like this:

echo trim($ourstring);

Keep in mind that trim() returns the trimmed string, so that it outputs "The Quick Brown Box Jumped Over The Lazy Dog". We can modify it so that trim() passes its return value to strlen() so that the code trims it and then outputs its trimmed length:

echo strlen(trim($ourstring));

PHP always executes the innermost functions first, so the previous code takes $ourstring, passes it through trim(), uses the return value of trim() as the parameter for strlen(), and prints it.

Of course, everyone knows that boxes do not jump over dogs — the usual phrase is "the quick brown fox." Fortunately, there is a function to fix that problem: str_replace(). Note that it has an underscore in it; PHP is inconsistent on this matter, so you really need to memorize the function name.

The str_replace() function takes three parameters: the text to search for, the text to replace it with, and the string you want to work with. When working with search functions, people often talk about needles and haystacks — in this situation, the first parameter is the needle (the thing to find), and the third parameter is the haystack (what you are searching through).

So, we can fix our error and correct box to fox with this code:

echo str_replace("Box", "Fox", $ourstring);

There are two little addendums to make here. First, note that we have specified "Box" as opposed to "box" because that is how it appears in the text. The str_replace() function is a case-sensitive function, which means it does not consider "Box" to be the same as "box". If you want to do a search and replace that is not case sensitive, you can use the stri_replace() function, which works in the same way.

The second addendum is that because we are actually changing only one character (B to F), we need not use a function at all. PHP enables you to read (and change) individual characters of a string by specifying the character position inside braces ({ and }). As with arrays, strings are zero-based, which means in the $ourstring variable $ourstring{0} is T, $ourstring{1} is h, $ourstring{2} is e, and so on. We could use this instead of str_replace(), like this:

<?php
 $ourstring = " The Quick Brown Box Jumped Over The Lazy Dog ";
 $ourstring{18} = "F";
 echo $ourstring;
?>

You can extract part of a string by using the substr() function, which takes a string as its first parameter, a start position as its second parameter, and an optional length as its third parameter. Optional parameters are common in PHP. If you do not provide them, PHP assumes a default value. In this case, if you specify only the first two parameters, PHP copies from the start position to the end of the string. If you specify the third parameter, PHP copies that many characters from the start.

We can write a simple script to print "Lazy Dog  " by setting the start position to 38, which, remembering that PHP starts counting string positions from 0, copies from the 39th character to the end of the string:

echo substr($ourstring, 38);

If we just want to print the word "Lazy", we need to use the optional third parameter to specify the length as 4, like this:

echo substr($ourstring, 38, 4);

The substr() function can also be used with negative second and third parameters. If you specify just parameter one and two and provide a negative number for parameter two, substr() counts backward from the end of the string. Rather than specifying 38 for the second parameter, we can use -10 so that it takes the last 10 characters from the string. Using a negative second parameter and positive third parameter counts backward from the end string and then uses a forward length. We can print "Lazy" by counting 10 characters back from the end and then taking the next four characters forward:

echo substr($ourstring, -10, 4);

Finally, we can use a negative third parameter, too, which also counts back from the end of the string. For example, using "-4" as the third parameter means to take everything except the last four characters. Confused yet? This code example should make it clear:

echo substr($ourstring, -19, -11);

That counts 19 characters backward from the end of the string (which places it at the O in Over) and then copies everything from there until 11 characters before the end of the string. That prints "Over The". The same thing could be written using -19 and 8, or even 29 and 8 — there is more than one way to do it!

Moving on, the strpos() function returns the position of a particular substring inside a string; however, it is most commonly used to answer the question, "Does this string contain a specific substring?" You need to pass it two parameters: a haystack and a needle (yes, that's a different order from str_replace()!).

In its most basic use, strpos() can find the first instance of "Box" in our phrase, like this:

echo strpos($ourstring, "Box");

This outputs 18 because that is where the B in Box starts. If strpos() cannot find the substring in the parent string, it returns false rather than the position. Much more helpful, though, is the ability to check whether a string contains a substring; a first attempt to check whether our string contains the word The might look like this:

<?php
 $ourstring = "The Quick Brown Box Jumped Over The Lazy Dog";
 if (strpos($ourstring, "The")) {
  echo "Found 'The'!n";
 } else {
  echo "'The' not found!n";
 }
?>

Note that we have temporarily taken out the leading and trailing whitespace from $ourstring and we are using the return value of strpos() for the conditional statement. This reads, "If the string is found, print a message; if not, print another message." Or does it?

Run the script, and you will see it print the "not found" message. The reason for this is that strpos() returns false if the substring is not found and otherwise returns the position where it starts. If you recall, any nonzero number equates to true in PHP, which means that 0 equates to false. With that in mind, what is the string index of the first The in the phrase? Because PHP's strings are zero-based and we no longer have the spaces on either side of the string, the The is at position 0, which the conditional statement evaluates to false — hence, the problem.

The solution here is to check for identicality. You know that 0 and false are equal, but they are not identical because 0 is an integer, whereas false is a Boolean. So, we need to rewrite the conditional statement to see whether the return value from strpos() is identical to false. If it is, the substring is not found:

<?php
 $ourstring = "The Quick Brown Box Jumped Over The Lazy Dog";
 if (strpos($ourstring, "The") !== false) {
  echo "Found 'The'!n";
 } else {
  echo "'The' not found!n";
 }
?>

Оглавление книги

Оглавление статьи/книги

Генерация: 0.475. Запросов К БД/Cache: 2 / 0
поделиться
Вверх Вниз