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.