Php regular expressions are actually big... Extract the following two rows of data & nbsp; 123 [test] abcc1c2c3 & nbsp; 456defd1 [test] d2d3 & nbsp; extracted to 123, abc, c1c2c3456 based on line breaks and spaces, def, d php regular expression specific, really big...
Extract the following two rows of data
123 [test] abc c1 c2 c3
456 def d1 [test] d2 d3
Separated by line breaks and spaces
Extracted
123, abc, c1 c2 c3
456, def, d1 d2 d3
Data may be ignored in the middle [*].
Evaluate the preg_match_all matching expression
------ Solution --------------------
Do you want preg_match_all or preg_replace?
What are those commas? Why are there no commas in the three following?
------ Solution --------------------
Regular, No ..
PHP code
$ Str = '123 [test] abc c1 c2 c3456 def d1 [test] d2 d3 '; $ ar = split ("[\ n \ r] +", $ str ); $ ar1 = explode ('', $ ar [0]); $ ar2 = explode ('', $ ar [1]); function fmt ($ ar) {foreach ($ ar as $ k => $ v) $ ar [$ k] = preg_replace ('/\[. * \]/', '', $ v); return $ ar;} $ ar1 = fmt ($ ar1); $ ar2 = fmt ($ ar2 ); $ str = implode ('', $ ar1 ). "\ n ". implode ('', $ ar2); echo"{$ Str} ";/* // Output123 abc c1 c2 c3456 def d1 d2 d3 */
------ Solution --------------------
Discussion
No, it's impossible. here we use the regular expression.
Preg_replace ('/\ [. * \]/', ', $ v );
Because there will be a lot of lines, I want to be efficient and express a little simpler!
------ Solution --------------------
If there are too many rows, you will not be afraid of it. the above code will be able to adapt to the variable rows of data with slight modifications.
------ Solution --------------------
PHP code
$ Str = '123 [test] abc c1 c2 c3456 def d1 [test] d2 d3789 ghi [sfjsldf] z1 z2 z3 '; $ ar = split ("[\ n \ r] +", $ str); $ arTMP = array (); foreach ($ ar as $ v) $ arTMP [] = explode ('', $ v); function fmt ($ ar) {foreach ($ ar as $ k => $ v) $ ar [$ k] = preg_replace ('/\[. * \]/', '', $ v); return $ ar;} foreach ($ arTMP as $ k => $ v) $ arTMP [$ k] = implode ('', fmt ($ v); $ str = implode (" \ n ", $ arTMP); echo"{$ Str} ";/* // Output123 abc c1 c2 c3456 def d1 d2 d3789 ghi z1 z2 z3 */
------ Solution --------------------
Dizzy .. As long as I see the regular expression, I will feel the first big ..
PHP code
$str = '123[test] abc c1 c2 c3456 def d1[test] d2 d3789 ghi[sfjsldf] z1 z2 z3';$str = preg_replace('/\[.*\]/','',$str);echo "{$str}";/*//Output123 abc c1 c2 c3456 def d1 d2 d3789 ghi z1 z2 z3*/