PHP string

A reference string

1. Strings containing single quotes.

// represents a single quote character string

$ name = ‘Mr.Lee’;

// variables include a single quote not resolve the

echo ‘This is $ name’;

// use the variable in an external connector string

echo ‘This is’ $ name;..

2 using double quotes string.

// no string variables, single and double quotes indiscriminate

$ name = ‘Mr.Lee’;

// variable double quotes analyzing

echo “This is $ name”;.

3 single quote escape character.

// use single quotes escaping slash

echo ‘This \’ s Mr.Lee ‘;

echo’ This is \\ Mr.Lee ‘;

4 escape character. double quotes.

In addition to resolve the variable double quotation marks, such as the two can escape quote itself with some more escape character. document may be stored as a whole string of multiple lines.

// multi-line string

$ info = <<< HERE

This is a document.

and a plurality of rows.



echo $ info;

output string

We have previously been explained more characters mode output string: echo, print_r, var_dump, var_export

etc., which are commonly used output method. Here, letMore other output methods are added.

1. Use the PRINT function to output.

// Output string

Print (‘output string’);

2. Use the Printf function to format the output string

3 . Use the sprintf function to format the output string

4. Format the output string

  1. The HTML entity is converted into a corresponding entity using the HTMLEntities function.

  2. Use the HTMLSpecialChars function to convert the entity set to generate legitimate HTML.

Use the htmlspecialchars_decode function to change the font to ordinary characters

HTML tag

1. Use the strip_tags function to delete the HTML tag

2. using the get_meta_tags function to get the metr tag metadata

3. Using the Get_Headers function to get the header information of the website

URL encoding

1. Use the RawurlenCode function to perform URL encoding

// using the RAWURLENCODE function, encoded, space and Chinese are encoded, English and The number is constant

2. Use the RawurDecode function to decode the URL string.

// String decoding of the encoded portion

3. Use the URLENCode function to encode the URL string.

/ / and RAWURLENCODE is just a space to encode spaces, not% 20

4. Using URLDECode decoding URL string

// decoding URL string

SQL encoding

1. Add a escape backslash using the AddSlashes function.

2. Use the stripsLashes function to delete the escape backslash.

Base64 encodes

The data is encoded using the base64_encode function.

  1. Use the base64_decode function to decode the data.

  2. URL encoded

Generate URL request string

using the http_build_query function

Use the PARSE_URL function to resolve the URL address

  1. String length

  2. 1. Use the strlen function to get the current String length.

  3. / / 18 characters string, space is also calculated

    // Under UTF8, one Chinese character accounts for 3 lengths

// under GBK, one Chinese characters accounted for 2 lengths

3. Using MB_STRLEN function

3. Using MB_Internal_Encoding to get the current character encoding

Clean the blank symbol

Use the TRIM function to clean up the string left and right blank

Size-to-write conversion

Use the STRTOLOWER function to convert the character to lowercase

  1. Use the start function to convert the character to uppercase

Using the UCFirst function

  1. using the LCFirst function

  2. will use the UCWORDS function to each The first letter of the word

  3. Use the MB_CONVERT_CASE function to perform case-in-write operation

  4. formatted Operation

  5. Use the NL2BR function to convert the resort to

  6. .

  7. Use the WordWrap function to enable strings to force wrap

using StrRev function in the specified location

  1. Replacement operation

  2. Returns the subtrings of the string using the SUBSTR function.

  3. 2. Use the MB_SUBSTR function to return string

  4. 3. Use the STR_REPLACE function to replace the specified string

4. Use the substr_replace function to replace the string

  1. 1. Use the STR_Repeat function to repeat the string

2. Use the STR_PAD function to fill the string

1. Use the strCMP function intoRow binary string comparison.

2. Use the strNCMP function to specify the length of the binary string comparison

3. Binary comparison of the natural sorting algorithm can be performed using the strnatcmp function

Find operation

1. Use the STRPOS function to find the first position

2. Using the STRPOS function to find the last appearance position

3. Using the strstr function Returns the first place where the string is first appearing and returns the following strings.

4. Use the STRRCHR function to find the last position of the string and return the string

, the statistical operation

1. Use the Substr_Count function statistical string

2. Using the str_word_count function to understand the usage of each word using the STR_WORD_COUNT function.

Decomposing operation

1. Use the Explode function to split the string.

// String

$ INFO = ‘this is a teacher!’;

// Split

$ array = evtern ‘, $ INFO);

Print_R ($ array);

2. Use the STR_SPLIT function to convert the string into an array.

// String

$ INFO = ‘this is a teacher!’;

// Divided according to each byte

Print_R ( STR_SPLIT ($ INFO, 1));

3. Use the chunk_split function to segment stringA small piece.

// String

$ INFO = ‘this is a teacher!’;

// Press 5 byte segmentation, and wrap

Echo chunk_split ($ INFO, 5, ‘


4. Use the Strtok function to split strings

// string

$ INFO =’ THIS Is A Teacher! ‘;

// For the first time, two parameters

$ TOKEN = STRTOK ($ INFO,’ ‘);

// Cyclic output, If the value is not false

while ($ token! = False) {

echo $ token. ‘


// Second only segmentation String, automatically get the remaining string

$ token = strtok (”);


5. Segmentation in the specified format using the SSCANF function.

// String

$ INFO = ‘this is a teacher! 30’;

// Split

$ array = sscanf $ INFO, ‘% S% S% S% S% D’);

// Output

Print_R ($ array);

Merging operation

1. Use the ImpLode function to merge strings.

/ / array

$ array = array (‘color’, ‘blue’, ‘Green’);

// divided by grouping an array element

$ INFO = IMPLODE (‘,’, $ array);

// Output

Echo $ info;

Encryption operation

1. Using the MD5 function to output 32-bit encrypted strings

2 using the MD5 function

2. Use the SHA1 function encrypted string output 40 Bit encryption password

3. Returns a verification integer using the CRC32 function

4. Generate a hash value

salt using the Hash function

salt Value operation

Since MD5 and SHA1, a simple encryption method has been cracked, so that you really want to achieve irreversible encryption, you need to add some things to

encrypted data. These things, we are called salt.

/ / Define a key

$ salt = ‘’;

// Password plain text

$ password = ‘123456’;

// Output Encryption

Echo MD5 ($ SALT. $ Password);

1. Use the ORD function to output the ASCII code value of the character.

/ / Output 97

Echo ORD (‘A’);

2. Use the CHR function to return the ASCII code character.

// Output A

Echo chr (97);

3. Use the str_shuffle function to randomly arrange strings.

// Random Arrange String

4. Use the StrPBRK function to match and return to the remaining string using the STRPBRK function.

// String

$ INFO = ‘this is a teacher!’;

// What character does it match which character

echo strpbrk ($ INFO, ‘MIT’);

5. Encrypts the string using the Crypt function.

// Set password

$ password = ‘mypassword’;

// Setting key

$ salt = ‘’;

// Get a hash value, add salt value

$ has Hash = Crypt ($ Password, $ SALT);

// Output

ECHO $ Hash;

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment