Working with strings

Working with strings in real life is really easy. Actions like Check if this string contains this or Tell me how many times this character appears are very easy to perform. But when programming, strings are concatenations of characters that you cannot see at once when searching for something. Instead, you have to look one by one and keep track of what the content is. In this scenario, those really easy actions are not that easy any more.

Luckily for you, PHP brings a whole set of predefined functions that help you in interacting with strings. You can find the entire list of functions at http://php.net/manual/en/ref.strings.php, but we will only cover the ones that are used the most. Let's look at some examples:

<?php

$text = '   How can a clam cram in a clean cream can? ';

echo strlen($text); // 45
$text = trim($text);
echo $text; // How can a clam cram in a clean cream can?
echo strtoupper($text); // HOW CAN A CLAM CRAM IN A CLEAN CREAM CAN?
echo strtolower($text); // how can a clam cram in a clean cream can?
$text = str_replace('can', 'could', $text);
echo $text; // How could a clam cram in a clean cream could?
echo substr($text, 2, 6); // w coul
var_dump(strpos($text, 'can')); // false
var_dump(strpos($text, 'could')); // 4

In the preceding long piece of code, we are playing with a string with different functions:

  • strlen: This function returns the number of characters that the string contains.
  • trim: This function returns the string, removing all the blank spaces to the left and to the right.
  • strtoupper and strtolower: These functions return the string with all the characters in upper or lower case respectively.
  • str_replace: This function replaces all occurrences of a given string by the replacement string.
  • substr: This function extracts the string contained between the positions specified by parameters, with the first character being at position 0.
  • strpos: This function shows the position of the first occurrence of the given string. It returns false if the string cannot be found.

Additionally, there is an operator for strings (.) which concatenates two strings (or two variables transformed to a string when possible). Using it is really simple: in the following example, the last statement will concatenate all the strings and variables forming the sentence, I am Hiro Nakamura!.

<?php
$firstname = 'Hiro';
$surname = 'Nakamura';
echo 'I am ' . $firstname . ' ' . $surname . '!';

Another thing to note about strings is the way they are represented. So far, we have been enclosing the strings within single quotes, but you can also enclose them within double quotes. The difference is that within single quotes, a string is exactly as it is represented, but within double quotes, some rules are applied before showing the final result. There are two elements that double quotes treat differently than single quotes: escape characters and variable expansions.

  • Escape characters: These are special characters than cannot be represented easily. Examples of escape characters are new lines or tabs. To represent them, we use escape sequences, which are the concatenation of a backslash () followed by some other character. For example, represents a new line, and represents a tabulation.
  • Variable expanding: This allows you to include variable references inside the string, and PHP replaces them by their current value. You have to include the $ sign too.

Have a look at the following example:

<?php
$firstname = 'Hiro';
$surname = 'Nakamura';
echo "My name is $firstname $surname.
I am a master of time and space. "Yatta!"";

The preceding piece of code will print the following in the browser:

My name is Hiro Nakamura.
I am a master of time and space. "Yatta!"

Here, inserted a new line. " added the double quotes (you need to escape them too, as PHP would understand that you want to end your string), and the variables $firstname and $surname were replaced by their values.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.126.199