Php blocks strings and php Strings
// How to divide the following strings into each small block, for example, 2017.2.14 \ n. We are all Chinese people \ n small Japan. For example, we are a small block $ str = '2017. 2.14 we are all Chinese Little Japan 2017.2.15 order cancellation information tag bug ha look at my recharge to the balance page 2017.2.16 modify bug load more Haha 2017.2.17 中中中 island little sheep ';
The final result is:
Okay. I will not talk about it anymore. Check the code --->
[First]:
// $partent1 = "/(\d{4}.\d{1,2}.\d{1,2}[\n\r]+[\x{4e00}-\x{9fa5}_a-zA-Z\n\r]+)/u";$partent2 = "/\d{4}.\d{1,2}.\d{1,2}(\S|\s[^(\d{4}.\d{1,2}.\d{1,2})])*/u";preg_match_all($partent2, $str, $match);foreach ($match[0] as $key => $value) { $match_tmp[] = explode("\n", $value);}p($match[0], $match_tmp, 'y');
Partent1 and partent2 can match the following data ::
However, what is different between partent1 and partent2 is that partent1 will match with \ n in the last match, as indicated by the Red Arrow:
Well, you should see why... different from the two regular expressions, partent1 uses the matching method to directly match the Chinese character after the date and then return it, while partent2 uses the matching method to take the matching method between the two dates, for example, in partent1, [\ x {4e00}-\ x {9fa5} _ a-zA-Z \ n \ r] matches \ n \ r. so this is the result of every non-last match .... you have to find a way to remove it here -----
Okay. The final result of the first method is:
{First type: --- correct}
[2]
Now let's take a look at the second method. This method is stupid, but intuitive. It is a string processing method.
Directly run the code :::
$ Arr = explode ("\ n", $ str); $ tmp = [];
// Retrieve the 2017 text here. here, you can also change the key and valueforeach ($ arr as $ key => $ value) with the date on January 14, 2017) {if (strstr ($ value, '123 ')) {$ tmp [$ key] [] = $ value; $ keyss [] = $ key; // [0, 3, 8, 11]}
// Then process -- logic: traverse the keyss when the next value of the pointer exists.
// When $ kk in $ arr is between $ keyss value, the $ arr value is taken out and put in the corresponding $ tmp array;
// If the next value of the pointer does not exist, it is the last value of the array, here, you only need to satisfy the condition $ v <$ kk and you can set foreach ($ arr as $ kk => $ vv) {foreach ($ keyss as $ k => $ v) {if (isset ($ keyss [$ k + 1]) {if ($ v <$ kk & $ kk <$ keyss [$ k + 1]) {$ tmp [$ v] [] = $ vv ;}} else {if ($ v <$ kk) {$ tmp [$ v] [] = $ vv ;}}} p ($ tmp, $ keyss, $ arr );
Then print the result to see:
{Second type: --- correct}
{Note: The p () method in it is a data output method written by yourself, just var_dump. You don't need to worry about it}
Summary: except for a little efficiency, the simplest method is usually the most convenient and quick. the two methods have their own lengths. partent1 In the first method needs to be improved, and there are too many loops in the second method. but each person has his/her own method. but do not use regular expressions. regular Expressions are more efficient than php methods... one of the most widely used Data Retrieval Applications in regular expressions is crawler technology, and the other is for matching and judgment, for example, matching a Mobile Phone mailbox or something.
There are still many shortcomings. If you have any questions, I 'd like to leave a message for improvement. Thank you very much!