PHP intercepts strings for problems

Source: Internet
Author: User
PHP intercepts string problems

In programming, you often encounter the problem of segmentation a string based on a word.

In general, we use explode to divide a string into an array and then proceed further.

But if there is a need to intercept a string in the middle of one of the two adjacent split characters, then using explode is a bit wasteful.

After all, many other intercepted strings are not available. Wasted space and wasted computing.


Today we found a way to use Preg_match. (if there is a similar, it is fate)


Here's how it's done:

function Getsubstr ($str, $dec, $index) {//Gets the string between the index separator in str and the index+1 delimiter     $index +=1;     $a = '/^ ([^ '. $dec. ']* '. $dec. ') {'. $index. '} /';     $match = Array ();     Preg_match ($a, $str, $match);     if (Isset ($match [1])) return RTrim ($match [1]);     Return ';}
?

The concrete principle does not explain, everybody sees to understand.

?

In addition, the other two methods are explode intercept the string array, and then take the value, and loop through the string, find the corresponding two separator position and then intercept the string.

?

Tested to get:

?

10w Line Record processing time:

??? The method of this article?? : 1.3521399497986

??? Explode?? : 1.4456930160522

??? Loop traversal?? : 1.4808049201965

?

The test results show that the 10W line record processing time is up to 0.1 seconds.

?

I wonder if there's any other way.

1/F llh110220 2012-09-19

Are 100,000 lines of text in one file? or 10w cycle time

2/F Liuzhiqiangruc 2012-09-19

Llh110220 wrote

Are 100,000 lines of text in one file? or 10w cycle time


Well, yes. 10w 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.