PHP 12: Manipulation of strings

Source: Internet
Author: User
Tags rtrim sprintf

Original: PHP 12: Manipulation of strings

This chapter describes the operation of strings.
The reason to separate the strings is because the strings are very important in each language. And it is a matter of concern to everyone.
We introduce the string in the following ways:

    1. The representation of a string.
    2. The special character of the string.
    3. Common operations for strings.
Let's say something specific.
1. The representation of a string
In most languages, double quotation marks are strings, and single quotes are characters. In PHP, however, there are 3 types of string representations. That
    • Single quotation marks
    • Double quotes
    • Delimiter (Heredoc syntax)
See, a single quote can actually be used to represent a string. So what if I want to represent single quotes? Like most languages, use escape symbols. That is, the backslash "\". So what's the difference between using single and double quotes? My point of view is that there is no much difference. The only difference is that double quotes can apply more escape characters.
Let's have a delimiter. Its syntax is "<<<". The usage is to provide an identifier after that, then provide the string after the identifier, and then provide the identifier after the string to end. For example:
1<?PHP
2 $str= <<<EOD
3Hello,This was an example forHEREDOC Syntax.
4attention to it.
5EOD;
6  Echo $str;
7?>Note that the provided identifier here is EOD, and the middle is the string.

2. The special character of the string
Start with an example.
Suppose you have a long string of words, such as a paragraph. After a few months, you find that your needs are changing, and you need to insert a variable somewhere in the conversation. This time if you re-use the string character is certainly quite complex. So is there any other way? Please be assured that PHP has taken this into account. That is the application of curly braces.
For PHP, it is usually parsed according to the following rules:
If you encounter a dollar sign ($), the parser will make as many utilises as possible to make up a valid variable name. If you want to explicitly specify the end of the name, enclose the variable name in curly braces. For example
1<?PHP
2$beer = 'Heineken';
3Echo "$beer ' s taste is great"; //works, "'" is a invalid character for Varnames
4Echo "He drank some $beers"; //won ' t work, ' s ' are a valid character for Varnames
5Echo "He drank some ${beer}s"; //Works
6 Echo "He drank some {$beer}s"; //Works
7?> Look at the 3rd, 4 lines. The third line of $beer ' s is actually $beer after adding "' s". Because the "'" Here is an invalid character for the name of the variable. But the $beers on line 4th is not the same. So the $beers is invalid.
In addition to the above, you will notice 5, 6 rows of ${beer} and {$beer}. They are all legal for PHP.
But you have to be careful, ${beer} and {$beer} must be close to the $ and {. Otherwise {It will be treated as a {character.

3, a common operation for strings.
I think this is a very exciting topic, and it is very practical.
The function of the string is much more, we still introduce a part of the function.

Sorting functions for strings
    • Chop (), in fact, is RTrim (). Of course RTrim () is still available.
    • LTrim () to remove more spaces to the left of the string.
    • Trim (), this comparison is relatively simple, that is, the string left and right 2 sides of the space are deleted.
Formatted output of a string
    • Practical HTML formatted. NL2BR (). Insert the <br> before the new line of the string.
<?PHP
Echo NL2BR("foo isn ' t\n bar");
?> The above string will be displayed in 2 lines, and if you remove this function, it will only appear on one line.
    • The format of the print output.
PHP supports the print () function, and as with Echo, the only difference is that print () returns an integer value that has been 1. In addition to print (), there are printf () and sprintf (). Write here, you will think, how and C like it. They work the same way, the difference is that printf () outputs the result to the browser, and sprintf () This is output to a variable.
Let
's look at their definition.
intprintf ( stringformat [, Mixedargs [, Mixed ]] )

string sprintf ( stringformat [, Mixedargs [, Mixed ]] )
    • Changes the case of a string.
Strtolower changing a string into lowercase
Strtoupper string into uppercase
Ucfirst capitalizes the first character of a string.
Ucwords the first character of each word in a string to uppercase.
    • Format characters so that they are stored.
It's about replacing quotes, backslashes, nulls, and so on, because they're sometimes not suitable for many occasions, such as storing to a database. To achieve this, addslashes is a good choice, and it uses backslashes to reference strings. In order to display the data, you need to call Stripslashes before the backslash is displayed.
    • string comparison
STRCMP,STRCASECMP,STRNATCMP, these 3 methods are sorted in dictionary order for example "2" to be greater than "12".
    • String length please call strlen.
    • String joins and splits
Explode is similar to the Split function in C #. For example:
1<?PHP
2$str = "is yours name O ' Reilly?";
3$var=Explode(" ",$str);
4Echo $var[0];//Output: is
5Echo $var[1];//Output: Yours
6?> Implode () and join () provide exactly the same functionality as explode (), which merges multiple characters together.
Strtok () is similar to split, but it only takes one out at a time.
To get the substring, call substr ().
    • Finding strings in a string
You can call Strstr (), STRCHR (), STRRCHR (), and Stristr (). Remember, STRCHR () is the alias of Strstr (), where the first occurrence is found. STRRCHR () to find where the last string appears. The difference between stristr () and Strstr () is that the former is case-insensitive.
    • You can use Strpos () and Strrpos () to find the location of the string.
    • Instead, call Str_replace () and Substr_replace ().
The above introduction is the most commonly used, if you want to get more string functions and functions of information, refer to the PHP function document.

PHP 12: Manipulation of strings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.