Chubby learns PHP summary 4-php string manipulation

Source: Internet
Author: User
Tags rtrim
1. String connection

The string is through the half-width period "." , you can concatenate two or more than two strings into a single string.

2. Remove string leading and trailing spaces and special characters

The trim () function is provided in PHP to remove whitespace and special characters from the left and right sides of the string, the LTrim () function removes whitespace and special characters from the string, and the RTrim () function is to remove the spaces and special characters that are to the rights of the string.

 
  ';  Remove all the special characters from the    echo trim ($str, "\r\r (::)"). '
'; Remove the special characters developed by Echo LTrim ($str, "\r\r"). '
'; Remove the special character set on the left Echo RTrim ($str, "@_@"). '
'; Remove the special character?> that is set on the right
3. Escaping and restoring string data

There are two types of string escapes and restores: the first is to manually escape, restore string data, and the second is to automatically escape and restore string data.

Manual escape, just need to add a backslash "\" on it, here's the auto-escaped function: Addslashes (String) adds \,addcslashes (string charlist) to the escape character in the string The function is escaped by implementing the character specified in the string, and Stripslashes () is used to restore the string escaped by the top two functions.

 
  ';    Echo ' $str 1 before escaping the string: '. $str 1. '
'; Echo ' $c escapes a specific string: '. $c. '
'; $b = Stripcslashes ($str 1); Echo ' $b The restored string: '. $b. '
'; Echo ' $a the string before the restore: '. $a. '
'; Echo '

';? >

4. Get string lengths to intercept strings, and string comparisons

 
  ';   The length of the string    echo substr ($str 2,0). '
'; Intercept all length strings from 0 position Echo substr ($str 2,4,10). '
'; The string is truncated from the fourth position, containing the fourth position, starting with the subscript 0 Echo substr ($str 2,-4,4). '
'; Intercept from the last fourth position, not including the fourth position of Echo substr ($str 2,0,-4). '
'; Intercept from No. 0 position, until the last fourth bit, including fourth bit //Two comparison string functions: strcmp (case sensitive), strcasecmp (case insensitive), strncmp (compares the first few strings in two strings); /STRSTR (Retrieves a string from the position of the first occurrence of the string to the end of the character); Strchr (the string is retrieved from the position of the string, as opposed to the former); Substr_count (retrieves the number of occurrences of a particular string in a string) //Replace function: Str_ireplace (search,replace,subject,int &count) Finding a string of strings and replacing them, search represents a string to find replacements, and replace represents the value to be replaced //subject represents the entire string, and count represents how many times the replacement work was performed, which is not case-sensitive. If you want to be case sensitive, use the Str_replace function //Replace function: Substr_replace (string,replace,start,length) Replace string string from start to length with replace Echo '

';? >

5. Formatting strings

 
  ';    Echo Number_format ($num, 2). '
'; Echo Number_format ($num, 2, ', ', '. '). '
'; Split string $str 3 = ' PHP Programming dictionary @net Programming Dictionary @asp Programming Dictionary @jsp Programming Dictionary '; $str _array = explode (' @ ', $str 3); Echo ' Split the string as: '; Print_r ($str _array); Echo '
'; Synthetic string echo ' Synthetic string function: '; Echo Implode ("compositing", $str _array). '
';? >



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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