PHP array and string conversion function collation Summary _php tips

Source: Internet
Author: User
Tags explode lowercase
1. Convert a string to an array
Str_split () is used to convert a string to an array
Syntax:
Copy Code code as follows:

Str_split (String,length)
<span style= "COLOR: #333333" ><span style= "font-size:12px" ><span style= "font-family: Song Body" >//</ Span></span></span>string is required, is the string to be segmented; <span style= "font-size:12px" ><span style= " Font-family: Song body; COLOR: #333333 "><span style=" line-height:28px ">
Length is optional, specify the lengths of each array element
</SPAN></SPAN></SPAN>

Tips:
Returns False if length is less than the 1,str_split () function.
If length is greater than the length of the string, the entire string is returned as the unique element of the array.
Example:
Copy Code code as follows:

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

2. String Split function
The explode () function splits the string into arrays.
Syntax:
Explode (Separator,string,limit)
Separator is required to specify the basis for dividing the split string, for example: "" (Space) "|" "," and so
String is required, is the strings to be manipulated
The limit is optional, which sets the maximum number of array elements returned.
Example:
Copy Code code as follows:

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

3. Convert an array to a string
Use the implode () function to combine array elements into a single string
Syntax:
Copy Code code as follows:

<pre>implode (Separator,array) </pre>//seperator is optional, which sets the content to be placed between array elements by default "" (Empty string) <BR>
Array is required, is to be combined into a string of arrays <BR>
<PRE></PRE>
Tips: Although the <EM>separator</EM> parameters are optional. However, for backward compatibility, it is recommended that you use two parameters. Implode () can receive two parameter order. But for historical reasons, explode () is no good. You have to make sure
The <EM>separator</EM> parameter is only 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. Find another character in a string species
Use the Strpos () or strstr () function
The Strpos () function returns the position where the string first appears in another string.
If the string is not found, it returns false.
Syntax:
Copy Code code as follows:

Strpos (String,find,start)
String is required to represent the searched strings
Find is required to represent the string being searched
Start optional. Specify where to start the search.

Tip:
This function is sensitive to case
Example:
Copy Code code as follows:

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

Output Results:
Copy Code code as follows:

4
8

5. Intercept some characters in a string
The substr () function returns part of the string
Syntax:
Copy Code code as follows:

SUBSTR (String,start,length)
String is required to specify the strings to return part of.
/* required. Specify where to start the string.
Positive number-starts at the specified position of the string
Negative number-starts at the specified position at the end of the string
0-Starts at the first character in the string
*/
/* Optional. Specify the length of the string to return. The default is until the end of the string.
Positive number-returns from the location where the start parameter is located
Negative-returns from the end of the string
*/

Tips:
If start is a negative number and length is less than or equal to start, length is 0.
examples;
Copy Code code as follows:

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

6. Get string length
The strlen () function is used to calculate the length of a string.
Example:
Copy Code code as follows:

<?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:
Copy Code code as follows:

<?php
$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:
Copy Code code as follows:

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