PHP split () string segmentation function Application Example

Source: Internet
Author: User
    1. $date = "08-30-2010";
    2. The delimiter can be a slash, a dot, or a horizontal line
    3. List ($month, $day, $year) = Split (' [/.-] ', $date);
    4. Output to a different time format
    5. echo "Today is: $year year $month month $day Day";
    6. ?>
Copy Code

Note that pattern is a regular expression, and if you want to use a split character that is a special character in a regular expression, first escape it.

You are 2 and divide the first four fields in the/etc/passwd:

Copy Code

If there are n items in a string that match pattern, the returned array will contain n+1 cells. For example, if pattern is not found, an array with only one cell is returned. Of course, this is true if the string is empty.

Example 3, parse the date that may be split with a slash, dot, or horizontal line:

    1. The delimiter can be a slash, a dot, or a horizontal line
    2. $date = "04/30/1973";
    3. List ($month, $day, $year) = Split (' [/.-] ', $date);
    4. echo "Month: $month; Day: $day; Year: $year
      \ n ";
    5. ?>
Copy Code

Note: pattern is a regular expression. If you want to use a split character that is a special character in a regular expression, first escape it. If you feel that the PHP function split () (or any other Regex function) behaves strangely, read the regex.7 file contained in the regex/subdirectory of the PHP release package. This file is a manual page format and can be read with commands like man/usr/local/src/regex/regex.7.

For a piece of text according to the word segmentation, because the text may be mixed in English and Chinese, and PHP function strlen can only calculate the number of bytes of the string, so that their own implementation of several functions, share under.

Example 1, calculates the total length of a character.

 
      0xa0) {$ccLen + +; $ind + +;} else{$ccLen + +;}} return $ccLen;}? >

Example 2, which intercepts a string from the left.

 
      

Example 3, the given text, by the number of cuts in the array (suitable for short text, long articles can be processed directly without a separate part)

 
      0) {$str _tmp=ccstrleft ($str _tmp, $smslen); Array_push ($arr _cont, $str _tmp); $i +=strlen ($str _tmp); $str _tmp=substr ($ Content, $i, strlen ($content));} return $arr _cont;} by bbs.it-home.org?>

Test:

 
      '; $smslen =3;//intercept length Print_r (splitcontent ($str, $smslen));? >

Split Result: Array ([0] + a calculation [1] = [2] = [+] = mixed 1[3] = 234[4] = string [5] = length [6] = abc[7] + D)

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