PHP Learning Note--6. String

Source: Internet
Author: User

1. How to define

① Double Quote method

Example: $str = "Hello";

Note: When you use double quotation marks for string definitions, you cannot have double quotes inside the string

Because PHP will parse it as a string boundary

Example: $str = "Hello," "Susan"; =>hello,

In this case, internal double quotes can be escaped, using \ "for non-semantic"

Then: $str = "hello,\" susan\ "";

Other symbols that need to be escaped:

\ "= =" \ n "+" \ \ =

\ r = Return \$ = $ \ ' = '

② Single-Quote method

PHP does not make complex escapes by using single-quote-defined strings

Only escape \ ' and \ \ All other outputs as-is

Single-quote string of $: Output directly as a string

and double quotes will try to parse it as a variable.

How do I select single/double quotes?

Large pieces of text, using single quotes, parsing fast

If you do want to enclose the variable in a string, you need to use double quotation marks

③heredoc notation

<<< identifiers

Large paragraph of text (containing double quotes, single quotes)

Identifier = = must have an exclusive line and no characters in front of it

Function: Consistent with double quotes, resolves internal escapes, variables, etc.

④nowdoc notation

<<< ' identifiers '

Large paragraph text

Identifier

Function: Consistent with single quotation marks

2. Class array attributes for strings

Example: $str = ' abcdef ';

echo $str [0]; =>a

3. String length function

strlen (string variable); = = Calculates the number of string bytes

Note: A Chinese character occupies three bytes in UTF-8

Mb_strlen (string variable, [character set]); = = Calculates the number of string characters

4. Finding the string neutron character position function

Strpos (string variable, ' find target ', [offset]); + = Calculates how many characters are the target characters from the beginning

Note: The object that the function looks for is case-sensitive

If the ' Find target ' fills in an integer, the integer is converted to an ASCII code

In the ASCII code:

65~90 = a~z 97~122 = a~z

If the first (0) is the target at the time of the If judgment, false is returned, and the = = = is used to judge

Example: if (Strpos () ===false) {

return false;

}else{

return true;

}

Offset is skipped by a certain number and then looked up

Stripos () = function Ibid., the target of which it is found is not case-sensitive

Strrpos () = The location of the last occurrence of the target character

Substr_count () = count the number of occurrences of the target

5. String substitution function

Str_replace (' Primitive ', ' replace ', string variable, [number of substitutions]);

' Primitive ' and ' replace ' are mixed types, such as arrays

If you replace two at a time, always start from scratch.

STRTR (string variable, ' primitive ', ' replace ');

' Primitive ' corresponds to ' replace with ', one character is substituted for one character

Example: ' Hi ' and ' ab ' are h=>a i=>b

In practical applications, full/half-width conversion is possible.

6. String intercept function

SUBSTR (string variable, starting from where, [to which end]);

From where to start if you fill in a negative number, proceed from right to left

Example: substr (' 123456 ', 2,-2); =>34

STRSTR (string variable, target character, [true]);

Intercept the first occurrence of the target character ~ end of the section

If you increase the true option, it is the ~ start part

STRRCHR (string variable, target character, [true]);

Intercept the last occurrence of the target character ~ end of the section

Can be used to determine file formats in real-world applications

7. Splitting, linking, and reversing functions of strings

Ltrim (string variable, ' need to trim character ');

Rtrim (string variable, ' need to trim character ');

Str_split (string variable, [n]);

Divides a string into a group of n letters, forming an array in the aggregate

Explode (' separator ', string variable);

Divides a string into groups with the specified delimiter, forming an array as a whole

Implode ([' Connector '], array variable);

Concatenate all cell values in an array into a single string with a connector

Chunk_split (string variable, several characters apart, [' Add character ']);

Example: Chunk_split ($STR, 3, ', '); = = Adds a comma to the $STR string every 3 characters

Increase the line break automatically if you do not fill in the added characters

Strrev (string variable); = = Invert string ABC=>CBA

8. String Escape function

Addslashes (string variable);

Special characters, such as single quotes, can be destructive when working in a database.

When the user enters a single quotation mark, the function can be escaped by adding \ in front of the single quotation mark

Stripslashes (string variable);

Convert special characters to HTML entities, such as < > = $LT; $GT;

Htmlspecialchars_decode (string variable);

Convert an HTML entity to a special character

9. String comparison function

strcmp (' String 1 ', ' String 2 '); = = Returns the bool value

10. String Conversion Functions

Strtolower (string variable); = = ALL Caps

Strtoupper (string variable); = = All lowercase

Ucfirst (string variable); = = First Letter capitalized

11. System functions

PathInfo (string variable); = = Returns its information, such as name, suffix, etc.

Number_format (string variable); = = The string is represented in thousands of bits 1,234,567

@zhnoah
Source: http://www.cnblogs.com/zhnoah/
This article is owned by me and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original
The right to pursue legal liability.

PHP Learning Note--6. String

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.