PHP Getting Started tutorial (19) PHP string processing

Source: Internet
Author: User
    1. $STR = "Hello World". It ' s a beautiful day. ";
    2. Print_r (Explode ("", $str));
    3. ?>
Copy Code

2, String Connection example:

    1. Defining strings
    2. $str 1 = "Hello world!";
    3. $str 2 = "PHP!";
    4. Connect the above two strings separated by a space
    5. $str 3 = $str 1. " " . $STR 2;
    6. Output the concatenated string
    7. echo $str 3;
Copy Code

3, the string interception substr (), mb_substr example:

    1. $str = "login.com";
    2. Echo substr ($STR, 2); Gin.com
    3. Echo substr ($str, 2,3); Gin
    4. Echo substr ($str,-2); Om negative number starts at the end of the fetch
    5. ?>
Copy Code

But when you intercept the Chinese string, it is easy to garbled, because a Chinese character is two bytes, and an English letter is a byte.

Workaround: 2,mb_substr (), use the same method and substr, but to open the php.ini inside the extension=php_mbstring.dll extension.

Example:

    1. echo mb_substr ("int Hello", 1,3, "UTF-8"); NT You
    2. ?>
Copy Code

3. String lookup Strpos () definition and usage 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)

Parameter description string required to specify the string to be searched find must specify the character to look for. Start is optional. Specify where to start the search

Example:

    1. $string = ' #11 #22#33#44#55#66#77 ';
    2. $find = ' #55 ';
    3. if (Strpos ($string, $find) ===false) {
    4. echo ' not found ';
    5. }else{//bbs.it-home.org
    6. echo ' Find ';
    7. }
    8. Echo Strpos ($string, $find);
    9. ?>
Copy Code

4, String substitution str_replace Example:

    1. $replace _string = ' You are bad, really bad ';
    2. $replace _string_b = ' very bad ';
    3. $replace _string_ = Str_replace ($replace _string_b, ' * ', $replace _string, $i);
    4. echo $replace _string_;
    5. ?>
Copy Code

Specify location Replacement Substr_replace

    1. Echo substr_replace ("Hello World", "PHP12", 6); Hello PHP12
    2. ?>
Copy Code

5, string-to-case function converts a string to lowercase strtolower (): The function converts all characters passed in the string argument to lowercase and returns the string in a small form. Example:

    1. $str = "I love You";
    2. $str = Strtolower ($STR);
    3. Echo $str;
    4. ?>
Copy Code

Output result: I love you

Convert character to uppercase Strtoupper (): This function, in contrast to the Strtolower function, converts the character of the passed character argument all to uppercase and returns the string in uppercase. Use the same as Strtolowe ().

Converts the first character of a string to uppercase Usfilst (): The function is to change the first character of the string to uppercase, which returns a string with the first character capitalized. Use the same as Strtolowe ().

Converts the first character of each word in a string to uppercase Ucwords (): The function capitalizes the first character of each word in the passed-in string. such as "Hello World", after the function is processed, will return "Hello Word." Use the same as Strtolowe ().

>>> See more PHP Getting Started tutorials <<<

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