PHP Learning (string manipulation)

Source: Internet
Author: User

In PHP, the definition of a string can be in English single quotation marks ‘ ‘ , or double quotation marks can be used " " . What's the difference between a single quote and a double quote?

PHP allows us to include it directly in a double quote string 字串变量 . The contents of a single quote string are always considered ordinary characters. PHP connects two strings in English with the dot number . . (Note the difference between the outputs)

1<?PHP2 $i= ' I ';3 $love= ' Love ';4 $you= ' You ';5 //connect a three string6 $hi=$i.$love.$you;7 Echo' $hi ';//$hi8 Echo"$hi";//I Love You9 Echo($hi);//I Love YouTen Echo $hi;//I Love You One?>
PHP string to remove the string from the end of the space

PHP has three functions to remove whitespace from a string

Trim removes spaces at both ends of a string.
RTrim is to remove the right space of a string, where r is the abbreviation.
LTrim is to remove the left space of a string, where L is the abbreviation for the Ieft.

The length of the string to get the PHP string

PHP has a magical function that can get the length of a string directly, and this function is strlen (). The strlen function is very good at calculating English characters , but if you have Chinese characters , what should I do to calculate the length?

You can use a mb_strlen() function to get the Chinese length in a string .

String Interception of PHP strings

PHP has a lot of string processing functions, including the string intercept function.

1. The Intercept function of the English string substr ()

Function Description: substr (string variable, start intercept position, intercept number)

2. Chinese string intercept function mb_substr ()

Function Description: Mb_substr (string variable, start intercept position, intercept number, page encoding)

PHP String Lookup string

If there is a string $str = ‘I want to study at imooc‘; , how do you find where the IMOOC is?

To find the string, we need to use PHP's lookup string function Strpos ();

Function Description: Strpos (the string to be processed, the string to locate, the starting position of the anchor [optional])

Replacement string for PHP string

Syntax: Mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ]);

< Span class= "type" > the function The returns a string or an array . The string or array is the subject all search are replace after replacing the result. Where count is specified, its value is set to the number of times the substitution occurs.

If search and to replace an array, then str_replace () will subject replace the mappings that do both. If replace the number of values is less than search the number, the extra substitution will be made using an empty string. If search it is an array and replace is a string, then the search substitution of each element in it will always use the string. The conversion does not change case .

If search and replace both are arrays, their values will be processed sequentially.

Merging and splitting strings of PHP strings

PHP string Merge function implode ()

Function Description: Implode (delimiter [optional], array)

Return value: Combines the elements of an array into a string, equivalent to the Array.prototype.join () method in JS.

PHP String separator function explode ()

Array explode ( string $delimiter , string $string [, int $limit ])

This function returns an array of strings, each of which is string a substring separated by a string delimiter as a boundary point.

If the limit parameter is set and is a positive number, the returned array contains the most limit elements, and the last element will contain string the remainder.

If the limit argument is a negative number, limit all elements except the last-element are returned.

If limit it is 0, it will be treated as 1.

PHP Learning (string manipulation)

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.