PHP array and string conversion function Usage Summary

Source: Internet
Author: User
The following is a detailed summary of the conversion function of the array and string in PHP, the need for friends can refer to the following

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

Str_split (string,length) <span style= "COLOR: #333333" ><span style= "font-size:12px" ><span style= " Font-family: The >//</span></span></span>string is a must, is the string to be divided; <span style= "font-size:12px" ><span style= "font-family: Song body; COLOR: #333333 "><span style=" line-height:28px ">//length is optional and specifies the length of each array element </span></span></ Span>

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

<?php$str= "www.baidu.com";p Rint_r (Str_split ($STR));? >


2. String splitting function
The explode () function splits the string into arrays.
Grammar:
Explode (Separator,string,limit)
Separator is required, which specifies the basis for splitting the split string, for example: "" (Space) "|" "," and so
String is required and is the one you want to manipulate
Limit is optional, which specifies the maximum number of array elements that are returned.
Example:

<?php$types= "Doc|docx|ppt|pptx|xls|xlsx|zip|rar";p rint_r (Explode ("|", $types)); >


3. Converting an array to a string
Use the implode () function to combine array elements into a single string
Grammar:

<pre>implode (Separator,array) </pre>//seperator is optional, which specifies what is placed between the elements of the array, and the default is "" (empty string) <br>//array is required, is the array to be combined into a string <br><pre></pre>tips: Although the <EM>separator</EM> parameter is optional. However, for backwards compatibility, it is recommended that you use two parameters. Implode () can receive two parameter sequences. But for historical reasons, explode () is not working. You must ensure that the <EM>separator</EM> parameter is not preceded by 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. Find another character in a string type
Use the Strpos () or strstr () function
The Strpos () function returns the position of the first occurrence of a string in another string.
If the string is not found, false is returned.
Grammar:

Strpos (String,find,start)//string is required to indicate that the string being searched is//find as must, indicating that the string being looked up is//start optional. Specify where to start the search.


Tip
The function is case sensitive
Example:

<?php$str= "HellO neo"; $find 1= "O"; $find 2= "O"; Echo strpos ($str, $find 1); echo "<br/>"; Echo Strpos ($str, $find 2 );? >

Output Result:

4
8


5. Intercepting some of the characters in a string
SUBSTR () function returns part of a string
Grammar:

substr (string,start,length)//string is required to specify a string to return a portion of. /* required. Specifies where to start the string.    positive number-starts negative at the specified position of the string    -starts at the specified position from the end of the string    0-*//* is selectable at the first character in the string. Specifies the length of the string to return. The default is until the end of the string.    positive number-Returns a negative number from the position where the start parameter is located    -returns from the end of the string */


Tips
If start is negative and length is less than or equal to start, length is 0.
Example

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

<?php$str= "Hello world!"; echo strlen ($STR);? >

Output is 12


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

<?php$str= "Hello world!"; echo Strtoupper ($STR);? >//output is: HELLO world!


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

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