官术网_书友最值得收藏!

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, \n represents a new line, and \t 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.\nI 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, \n 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.

主站蜘蛛池模板: 晋城| 永丰县| 广东省| 东莞市| 固镇县| 宁明县| 连南| 小金县| 婺源县| 昭苏县| 德江县| 开平市| 安达市| 铜川市| 满城县| 景宁| 北宁市| 塔城市| 阿瓦提县| 潞西市| 阜阳市| 桂东县| 迁西县| 搜索| 富蕴县| 天气| 虹口区| 大邑县| 芦溪县| 观塘区| 汉寿县| 大理市| 大姚县| 开江县| 松原市| 雅江县| 寿光市| 镶黄旗| 隆回县| 响水县| 砚山县|