Php array and string conversion functions

Source: Internet
Author: User


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

<SPAN <? Php

$ Str = "www.jbxue.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 and specifies the maximum number of returned array elements.
Example:

$ 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:

// Array is required and must be combined into an array of strings <BR>
<PRE> </PRE>
Tips: although the <EM> separator </EM> 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
The <EM> separator </EM> parameter must be set before the <EM> string </EM> parameter. Example: <PRE class = "php" name = "code"> <? Php
$ Arr = array ('hello', 'World! ', 'Beautiful', 'day! ');
Echo implode ("", $ arr );
?> </PRE> <BR>
<PRE> </PRE>

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:

// String is required, indicating the string to be searched
// Find is required, indicating the string to be searched
// Start is optional. Specifies the location where the search starts.

Tip:
This function is case sensitive.
Example:

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

Output result:
Copy the Code as follows:

4
8

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

// 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;

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

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

$ 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:

$ 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:

$ 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.