PHP string processing

Source: Internet
Author: User

The 1.strpos () function is used to find a character or a specified text within a string.

If a match is found in the string, the function returns the first matching character position. If no match is found, FALSE is returned .

<?phpecho Strpos ("Hello world!", "World");? > Return: 6

2. The strlen () function returns the length (number of characters) of the string.

<?phpecho strlen ("Hello world!");? > Return: 12

3. The Str_split () function splits the string into an array.

<?php//three a group into an array of  Print_r (Str_split ("Hello", 3)); >

 Array ([0] = Hel [1] = lo) 

4.explode ()

<?PHP$STR = "Hello World W3cschool";p rint_r (Explode ("", $str));? >

Results:

Array (    [0] = Hello    [1] =    [2] = = W3cschool)

5. Replace the character "world" (case insensitive) in the string "Hello world!" with "Peter": Str_ireplace () "Case insensitive"

Replace the character "world" (case-insensitive) in the string "Hello world!" with "Peter": Str_replace () "Case Sensitive"

<?phpecho Str_replace ("World", "Peter", "Hello world!");? >

6. substr (string,start,length) Start: Index

<?phpecho substr ("Hello World", 6);? >

Output: World

7.strstr (string,search,before_search)------stristr (string,search,before_search) is case insensitive

String Necessary. Specifies the string to be searched.
Search Necessary. Specifies the string to search for. If the parameter is a number, the character that matches the ASCII value of the number is searched.
Before_search Optional. A Boolean value with the default value of "false". If set to "true", it returns the string portion of the first occurrence of the search parameter.
<?phpecho strstr ("Hello world!", "World");? > Output: World

The third one if true indicates that all values before the string are returned otherwise the found string is returned

<?phpecho strstr ("Hello world!", "World", true);? >  Output: Hellow

8. Replacing strings

<?phpecho substr_replace (' abcdef ', ' # # # ', 1);//Output a## #echo substr_replace (' abcdef ', ' # # # ', 1, 2);//Output a## #defecho Substr_replace (' abcdef ', ' # # # ',-3, 2);//Output abc## #fecho substr_replace (' abcdef ', ' # # # ', 1,-2);//output a## #ef?>

 

PHP string processing

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.