Computers, Programming
PHP: working with strings. PHP String Functions
Sites can be divided into static and dynamic. After mastering HTML and CSS, which allow you to make a beautiful business card on the Internet, many people are thinking how to create a dynamic site in PHP. In this case, the layout should take into account that now he begins to study web programming: the principles of working with the site will differ. One of the first problems that a beginner in PHP faces is working with strings, reading and processing them.
Creating and displaying a string
We will parse the output of a string to the screen using the familiar echo language construct. A programmer can output a string at once:
Echo "This is New String"
Or first create a variable, and then display it on the screen:
$ Str = "This is the New String";
Echo $ str;
If you need to display several lines in one, then they resort to their concatenation:
Echo "It". "New". " Line";
or
$ Str1 = "This";
$ Str2 = "New";
$ Str3 = "Line";
Echo $ str1. $ Str2. $ Str3;
In the latter case, the NewNet line will be displayed. You can add a space immediately by calling echo:
Echo $ str1. ''. $ Str2. ''. $ Str3;
In this case, the screen will display: "This is the New Line". Concatenation is possible not only when outputting, but also when creating a string:
$ Str1 = "This";
$ Str2 = "New";
$ Str3 = "Line";
$ String = $ str1. ''. $ Str2. ''. $ Str3;
Echo $ string;
Echo displays both Latin letters and Cyrillic letters . If one of the variables contained a number, then when concatenated, this number will be converted to the corresponding line:
$ I = 2;
$ Sum = $ i + $ i; // now $ sum contains the number 4
Echo $ i. "+". $ I. "=". $ Sum;
The screen will display: "2 + 2 = 4".
Service symbols
Let's say a string is defined using double quotes ($ string = "That's it"). Then you can use the control sequences quite calmly:
- \ N performs a line break;
- \ R returns the carriage;
- \ "Escapes double quotes:
- Echo "A string with \" double quotes "; // A string with double quotes
- \ $ Shields the dollar;
- \\ escapes the backslash.
Sequences are much more, all of them can be found in the official documentation of PHP.
How to find the position of the first occurrence of a substring
Let's say we have a simple line:
$ String = "My name is Yemelyan and I am 27 year old";
Also we have two lines with the names:
$ Name = "Yemelyan";
$ AnotherName = "Katherin";
We need to find out if the first line contains these two names. To do this, use the function strpos ($ str, $ search). It returns the position of the search substring $ search, if this line is contained in the original, $ str. Otherwise, the function returns a Boolean value of false. For example, strpos ($ string, $ anotherName) will return false, and strpos ($ string, $ name) is an integer. The code will be like this (we'll write an option when the position is displayed on the screen):
$ String = "My name is Yemelyan and I am 27 year old";
$ Name = "Yemelyan";
$ AnotherName = "Katherin";
Echo strpos ($ string, $ anotherName); // print false
Echo strpos ($ string, $ name); // print the position of the first occurrence of the substring
Note that the line numbering starts from zero, that is, in our case, the last line will print the number 11 (spaces are also considered).
Finding the position of the last occurrences of substrings and pitfalls
If the function strpos () returns the position of the first occurrence, then the inverse strrpos () function looks for the last occurrence of the substring.
There are some pitfalls associated with the beginning of the numbering. This should be taken into account: in PHP, working with strings can be complicated by limitations in comparisons. So, it's better not to use the comparison operation with negation: strpos ($ str, $ search)! = False. In any version of PHP, examples with such an equivalent may not work correctly, because the line numbering starts from zero, and in the logical interpretation 0 is false. This also applies to the strrpos () function.
How to find the number of occurrences of a substring
It is often necessary to find not the position of the first or last occurrence of a substring in a string, but the total number of substrings. To do this, use the function substr_count (), which processes at least two variables: substr_count ($ str, $ search). Returns an integer. If you want to reduce the search scope by a line, then two more variables are passed to the function: the beginning and end of the line, respectively. That is, the function in this case is called so: substr_count ($ str, $ search, $ start, $ end). The function will look for a substring $ search in the interval from $ start to $ end of the original string $ str. If the string is not found, the function returns zero.
How to change the case of a string in PHP: examples
Changing the register is often used to compare strings and conditional statements. Suppose a user should enter the name of the supreme god in Scandinavian mythology. The program has the option "One", with which the user's response will be compared. If the entered text does not match the one you have (for example, the user will write "one" or "ONE"), the program will return false instead of true. To avoid this, the register change function is used. This is often used if the site in PHP has tags: instead of hundreds of variants of the word "personal" ("personal", "personal", "personal", etc.), there is only one tag in lowercase.
The strtolower () function changes the case to the lower one. Suppose there is a line $ catName = "Fluffy". The strtolower ($ catName) function returns the string "fluffy". Change the register to the upper one using the strtoupper () function.
How to find the length of a string in PHP: working with functions
It is often required to find the length of a string. For example, in PHP, you might need to work with lines of this kind in creating a loop. To find a string, use the strlen () function, which returns a number-the number of characters. We must not forget that the last character will have the strlen ($ str) -1 number, since the numbering starts from zero.
Getting and replacing substrings in PHP: working with strings
The substring is obtained by substr (), which can take two or three arguments: substr ($ str, $ start, $ end). Let's say we have the string $ string = "Fluffy cat", and we want to get a substring from the second to the fourth character. Since the numbering starts from zero, the variable with this substring will look like this: $ newString = substr ($ string, 1, 4). If we enter $ newString = substr ($ string, 1), we get a substring from the second character to the last character (that is, "luffy"). This code is identical to the full line code using strlen (): substr ($ string, 1, strlen ($ string)).
To replace a substring, use the function str_replace (), which takes three variables: str_replace ($ subStr, $ newSub, $ str). Unlike many functions, str_replace () works correctly with Cyrillic symbols and has no analog with a prefix. Example:
$ Str = "Today is terrible weather!";
$ NewStr = str_replace ("awful", "wonderful", $ str); // Today is wonderful weather!
Translation of a line to a number
To use both intval () and floatval (), it is necessary that the string starts with digits, and they will be converted to a number. If after the numbers any set of letters goes, they are simply ignored. If the string begins with letters, the use of the function will return zero. Ideally, the line should contain only numbers.
Converting a number to a string
Often you need to translate numbers into a string. For example, if you want to take a half of a number and square it up (for example, check if the equality is fulfilled: 88 x 88 + 33 x 33 = 8833). In this case, the function strval () is used, which returns a string with a number. After that, with the new line, you can perform all the other actions: modify, search for the occurrence of substring and other functions. If necessary, the line can be translated into a number as described above.
Similar articles
Trending Now