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.