Php string truncation

Source: Internet
Author: User
In programming, the php string truncation problem is often caused by splitting a string based on a specific character. In general, we use explode to split the string into an array and perform further operations. However, if one requirement is to intercept one or two adjacent strings in a string to be separated, it would be a waste to use explode. After all, many other intercepted strings cannot be used. Php string truncation

In programming, we often encounter the problem of splitting a string based on a specific character.

In general, we use explode to split the string into an array and perform further operations. However, if one requirement is to intercept one or two adjacent strings in a string to be separated, it would be a waste to use explode. After all, many other intercepted strings cannot be used. Space and computing are wasted.
Today we found a method that uses preg_match. (If there are similarities, it is fate)
The specific method is as follows:
Function getsubstr ($ str, $ dec, $ index) {// Obtain the string $ index + = 1 between the index separator and index + 1 separator in str; $ a = '/^ ([^ '. $ dec. '] *'. $ dec. '){'. $ index. '}/'; $ match = Array (); preg_match ($ a, $ str, $ match); if (isset ($ match [1]) return rtrim ($ match [1]); return '';}
?

I will not explain the specific principles. everyone can understand them.

?

In addition, the other two methods are explode to capture the string array, then take the value, and traverse the string cyclically, find the corresponding two separators and then intercept the string.

?

Tested:

?

Processing time of 10 million rows of records:

??? How does this document work ?? 1.3521399497986

??? Explode ?? 1.4456930160522

??? Loop traversal ?? 1.4808049201965

?

The test results show that the processing time of records in 10 million rows is 0.1 seconds at most.

?

I wonder what other methods are there?

1 floor llh110220 2012-09-19 0.1 million are all in one file? Or will the liuzhiqiangruc 2012-09-19 llh110220 on the second floor of the 10w cycle time write that 0.1 million of the texts are in one file? Or 10 million cycles
Well, all 10 million lines are in a text file.

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.