Phpsplit () string segmentation function example

Source: Internet
Author: User
Tags ereg
Phpsplit () string segmentation function example

  1. $ Date = "08-30-2010 ";
  2. // The delimiter can be a diagonal line, a dot, or a horizontal line.
  3. List ($ month, $ day, $ year) = split ('[/.-]', $ date );
  4. // The output is in another time format.
  5. Echo "today is: $ year, $ month, $ day ";
  6. ?>

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

You 2 split the first four fields in/etc/passwd:

If the string contains n items that match the pattern, the returned array contains n + 1 units. For example, if no pattern is found, an array with only one unit is returned. Of course, this is also true if the string is empty.

Example 3: parse a date that may be separated by diagonal lines, points, or horizontal lines:

  1. // The delimiter can be a diagonal line, 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. ?>

Note: pattern is a regular expression. If you want to use a delimiter that is a special character in a regular expression, you must first escape it. If the PHP function split () (or any other regex function) is abnormal, read the regex.7 file contained in the regex/subdirectory of the PHP release package. This document is in the format of the manual page. you can read it using commands similar to man/usr/local/src/regex/regex.7.

Split a piece of text by words, because the text may be a mix of Chinese and English, and the php function strlen can only calculate the number of bytes of the string, so I implemented several functions and shared them.

Example 1: calculate the total length of a character.

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

Example 2: extract a string from the left.

     

Example 3: store the given text into an array based on the number of slices (suitable for short text, which can be directly processed once if a long article is not separated)

     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; // capture length print_r (SplitContent ($ str, $ smslen);?>

Split result: array ([0] => a computing [1] => Chinese and English [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.