Php array and string conversion functions

Source: Internet
Author: User
The following is a detailed summary of the array and string conversion functions in php. For more information, see

The following is a detailed summary of the array and string conversion functions in php. For more information, see

1. convert a string into an array
Str_split () is used to convert a string into an array.
Syntax:

The Code is as follows:


Str_split (string, length)
//String is required. The website space is the string to be separated;
// Length is optional, specifying the length of each array element


Tips:
If the length is less than 1, The str_split () function returns false.
If the length is greater than the length of the string, the entire string is returned as a unique element of the array.
Example:

The Code is as follows:


$ Str = "www.baidu.com ";
Print_r (str_split ($ str ));
?>


2. string segmentation Functions
The explode () function separates strings into arrays.
Syntax:
Explode (separator, string, limit)
// Separator is required and specifies the basis for string segmentation, for example, "" (Space) "| "","
// String is a required string
// Limit is optional. It specifies the maximum number of returned array elements in the website space.
Example:

The Code is as follows:


$ Types = "doc | docx | ppt | pptx | xls | xlsx | zip | rar ";
Print_r (explode ("|", $ types ));
?>


3. convert an array into a string
Use the implode () function to combine array elements into a string.
Syntax:

The Code is as follows:


implode(separator,array)
// Seperator is optional and specifies the content to be placed between array elements. The default value is "" (Null String)

// Array is required and must be combined into an array of strings.

 

Tips: Although SeparatorThe parameter is optional. However, we recommend that you use two parameters for backward compatibility. Implode () can receive two parameter sequences. However, explode () cannot be used for historical reasons. You must ensure
SeparatorParameters in StringBefore the parameter. Example:
 $arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>


 


4. Search for another character in the string type
Use strpos () or strstr () Functions
The strpos () function returns the position where the string first appears in another string.
If this string is not found, false is returned.
Syntax:

The Code is as follows:


Strpos (string, find, start)
// String is required, indicating the string to be searched
// Find is required. The Hong Kong server is rented to indicate the string to be searched.
// Start is optional. Specifies the location where the search starts.


Tip:
This function is case sensitive.
Example:

The Code is as follows:


$ Str = "HellO neo ";
$ Find1 = "O ";
$ Find2 = "o ";
Echo strpos ($ str, $ find1 );
Echo"
";
Echo strpos ($ str, $ find2 );
?>


Output result:

The Code is as follows:


4
8


5. truncate some characters in the string
The substr () function returns part of the string.
Syntax:

The Code is as follows:


Substr (string, start, length)
// String is required, which specifies that a part of the strings should be returned.
/* Required. Specifies where the string starts.
Positive number-start at the specified position of the string
Negative number-starting from the specified position at the end of the string
0-Start at the first character in the string
*/
/* Optional. Specifies the length of the string to be returned. The default value is until the end of the string.
Positive number-return from the position of the start Parameter
Negative number-return from the end of the string
*/


Tips:
If start is a negative number and the length is less than or equal to start, the length is 0.
Example;

The Code is as follows:


$ Str = "Hello world! ";
Echo substr ($ str, 0 );
Echo"
";
Echo substr ($ str, 6, 5 );
?>


6. Get the string length
The strlen () function is used to calculate the length of a string.
Example:

The Code is as follows:


$ Str = "Hello world! ";
Echo strlen ($ str );
?>
// The output result is 12.


7. Convert the string to uppercase
The strtoupper () function converts a string to uppercase.
Example:

The Code is as follows:


$ Str = "Hello world! ";
Echo strtoupper ($ str );
?>
// The output result is: hello world!


8. Convert the string to lowercase.
The strtolower () function converts a string to lowercase.
Example:

The Code is as follows:


$ Str = "Hello World! ";
Echo strtolower ($ str );
?>

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.