PHP: Working with strings.

sites can be divided into static and dynamic.After mastering HTML and CSS, which allow to make beautiful business card on the Internet, many are thinking how to create a dynamic site in PHP.This coder has to take into account that now he begins to learn web programming: how to work with the site will be different.One of the first challenges faced by novice in PHP - working with strings, their reading and processing.

is worth noting that in PHP String functions involve a large number of methods, so their study is to start with the most simple manipulation such as writing a string, search, obtaining or replacing a substring, changing case, and return the length of the string.Many of the functions do not work with Cyrillic characters.Therefore, all examples are written in English for clarity.For Cyrillic line uses the same functions, but with the prefix mb_ (eg, mb_strpos ()).Before using analogues, in php.ini, you must uncomment the line; extension = php_mbstring.dll, simply by removing the semicolon.

Creation and line

We analyze the string output using all known language construct echo.A programmer can put a line right:

echo "This new Line"

or first create a variable, and then display it:

$ str = "This is a new string";

echo $ str;

If you want to display multiple lines in one, then resorted to their concatenation:

echo "It is.""New"." Line";

or

$ str1 = "It";

$ str2 = "new";

$ str3 = "string";

echo $ str1.$ str2.$ str3;

In the latter case will be displayed EtoNovayaStroka .Space can be added immediately by a call echo:

echo $ str1.''.$ str2.''.$ str3;

In this case, the screen displays: "This is a new line."Concatenation is possible not only in the output, but also to create a string:

$ str1 = "It";

$ str2 = "new";

$ str3 = "string";

$ string = $ str1.''.$ str2.''.$ str3;

echo $ string;

Echo displays both letters and Cyrillic.If one of the variables containing numbers, when concatenating the number will be converted to the appropriate line:

$ i = 2;

$ sum = $ i + $ i;// now $ sum contains the number 4

echo $ i."+".$ i."=".$ sum;

You will see: the "2 + 2 = 4".

Service symbols

For example, the string is defined by a double-quote ($ string = "This is how").Then you can quite happily use the escape sequences:

  • \ n commits a newline;
  • \ r carriage returns;
  • \ "escapes the double quotation marks:
    • echo" The string \ "double \" quotes "; // string with the" double "quotes
  • \ $ screens dollar;
  • \\ backslash escapes.

Strings much more, all their possible to find in official documentation of PHP.

Find position of first occurrence

Suppose we have a simple line:

$ string = "My name is Yemelyan and I am27 year old ";

We also have two rows with the names:

$ name =" Yemelyan ";

$ anotherName =" Katherin ";

We need to know whether the first row of thetwo names. To do this, use the function strpos ($ str, $ search). It returns the position of the desired substring $ search, if the string is contained in the initial, $ str.Otherwise, the function returns a boolean value to false.For example, strpos ($ string, $ anotherName) returns false, and the strpos ($ string, $ name) - an integer.The code will be the (write option, where the position is displayed):

$ string = "My name is Yemelyan and I am 27 year old";

$ name = "Yemelyan";

$ anotherName = "Katherin";

echo strpos ($ string, $ anotherName);// outputs false

echo strpos ($ string, $ name);// displays the position of first occurrence

Note that line numbering starts at zero, that is, in this case, the last line will show the number of 11 (spaces are also considered).

search position of the last occurrence of a substring and pitfalls

If the strpos () returns the position of the first occurrence, the inverse function strrpos () searches for the last occurrence of a substring.

There are some pitfalls associated with the beginning of the numbering.It should take into account: In PHP work with lines may be complicated by limitations in comparisons.So, it is better not to use a comparison operation with a negation: strpos ($ str, $ search)! = False.In any version of PHP with examples of such equivalence may not work correctly, because the line numbering starts at zero, and 0 in the logical interpretation is false.This also applies to the function strrpos ().

How to find a number of occurrences of a substring

often do not need to find the position of the first or last occurrence of a substring in a string, and their total number.To do this, use the function substr_count (), which handles at least two variables: substr_count ($ str, $ search).It returns an integer.If you want to reduce the scope of the search on the line, the function is passed two variables: the beginning and end of the line, respectively.That is, the function in this case is called as: substr_count ($ str, $ search, $ start, $ end).The function will search the substring $ search in the range from $ start to $ end of the string $ str.If the string is not found, the function returns zero.

How to change the case of the string in PHP: Examples

Change case is often used to compare strings and conditional statements.For example, the user must enter the name of the supreme god in Norse mythology.The program is a version of "One", which will be compared and the user's response.If the text entered does not match with the available (for example, the user writes a "one" or "one"), the program will return false instead of true.To avoid this, use the function change the case.It is often used if the site is in PHP tags: instead of hundreds of variants of the word "private" ("Private", "private", "personal" and so on. N.) There is only one tag in lowercase.

strtolower () function changes the case to the lower.Let's have a string $ catName = "Fluffy".Function strtolower ($ catName) returns the string "fluffy".Change case to upper, you can use the function strtoupper ().

How to find the length of the string in PHP: Working with functions

often required to find the length of the string.For example, in PHP work with strings of this kind may be required in the creation cycle.To search for a string function is used strlen (), which returns a number - the number of characters.We must not forget that the last character will have a number strlen ($ str) -1, as the numbering starts with zero.

Production and replacement of a substring in PHP: Working with strings

Getting substring function performed substr (), which can take two or three arguments: substr ($ str, $ start, $ end).Let's say we have a string $ string = "Fluffy cat", and we want to get a substring from the second to the fourth character.Since numbering starts at zero, the variable with that substring will look like this: $ newString = substr ($ string, 1, 4).If we introduce the $ newString = substr ($ string, 1), we get a substring from the second to the last character (ie "luffy").This code is identical to the full code string using strlen (): substr ($ string, 1, strlen ($ string)).

used to replace the substring function str_replace (), which takes three variables: str_replace ($ subStr, $ newSub, $ str).Unlike many functions, str_replace () works correctly with Cyrillic characters and has no counterpart with the prefix.Example:

$ str = "Today terrible weather!";

$ newStr = str_replace ("terrible", "wonderful", $ str);// Today, wonderful weather!

Translation string to a number

Anyone who is studying web programming, sooner or later have to translate the string into a number.For this purpose, two similar functions: intval () and floatval (), each of which takes one variable $ string.From each other they differ only in the type of data returned: intval () returns an integer, and floatval () - floating-point number.

for use intval (), and floatval () requires that the line starts with a number, and they will be converted to a number.If the numbers will go any set of letters, they just ignore it.In that case, if the line begins with the letters, the use of the function will return zero.Ideally, the line must contain only digits.

Translation of a string

often required to transfer the number to a string.For example, if you want to take half of it and to erect a square (for example, check whether the equality: 88 x 88 x 33 + 33 = 8833).In this case the function strval (), which returns a string with the number.After this, a new line, you can perform all other actions: edit, search for the occurrence of the substring, and other functions.If necessary, the line can be re-transferred to those already described above.

The article was considered only a small part of all the functions associated with strings.Part undescribed functions working with characters, but most had not been included in the material due to the specificity.To view these functions need to proceed to read the official documentation on PHP, which displays current information.