PHP array and string conversion functions Daquan _php Tutorial

Source: Internet
Author: User

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

<>< span="">

$str = "www.jbxue.com";
Print_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:
<>< span="">

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

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

Implode (Separator,array)//seperator is optional, which specifies what is placed between the elements of the array, by default "" (Empty string)

An array is required and is a string of arrays to be combined


Tips: Although SeparatorThe parameters are 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 have to promise.
SeparatorParameters in stringParameter before the line. Example:
 
    $arr = Array (' Hello ', ' world! ', ' Beautiful ', ' day! ');
echo Implode ("", $arr);
?>


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:
< span="">

String is a must, representing the searched strings
Find is required to represent the string being looked up
Start is optional. Specify where to start the search.

Tip
The function is case sensitive
Example:
<>< span="">

$str = "HellO Neo";
$find 1= "O";
$find 2= "O";
Echo Strpos ($str, $find 1);
echo "
";
Echo Strpos ($str, $find 2);
?>

Output Result:
Copy the code code as follows:

4
8

5. Intercepting some of the characters in a string
SUBSTR () function returns part of a string
Grammar:
< span="">

string is required, which specifies the strings to be returned as part of.
/* required. Specifies where to start the string.
Positive number-starts at the specified position in the string
Negative number-starts at the specified position from the end of the string
0-Starts at the first character in a string
*/
/* Optional. Specifies the length of the string to return. The default is until the end of the string.
Positive number-returns from the position where the start parameter is located
Negative-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
<>< span="">

$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:
<>< span="">

$str = "Hello world!";
echo strlen ($STR);
?>
Output is 12

7. Convert a string to uppercase
The Strtoupper () function converts a string to uppercase.
Example:
<>< span="">

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

8. Convert a string to lowercase
The Strtolower () function converts a string to lowercase.
Example:
<>< span="">

$str = "Hello world!";
echo Strtolower ($STR);
?>

http://www.bkjia.com/PHPjc/440226.html www.bkjia.com true http://www.bkjia.com/PHPjc/440226.html techarticle 1. Convert a string to array str_split () to convert a string to the array syntax: SPAN php $str = "www.jbxue.com"; Print_r (Str_split ($STR)); 2. String splitting function ...

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