PHP Regular expression specific, really big head ...
Extract the following two rows of data
123[TEST] ABC C1 c2 c3
456 def D1[test] D2 D3
Split with line breaks and spaces
Request Extraction to
123,abc,c1 C2 C3
456,DEF,D1 D2 D3
In the middle of the data there may be [*] to ignore
Finding Preg_match_all matching expressions
------Solution--------------------
To Preg_match_all or preg_replace?
What are those commas about? Why are there no commas behind the three?
------Solution--------------------
Regular, No.
PHP Code
$str = ' 123[test] ABC C1 c2 c3456 def D1[test] D2 d3 '; $ar = Split ("[\n\r]+", $str); $ar 1 = Explode (', $ar [0]); $ar 2 = Explod E (', $ar [1]), function fmt ($ar) {foreach ($ar as $k + $v) $ar [$k] = preg_replace ('/\[.*\]/', ', $v); return $ar;} $ar 1 = FMT ($ar 1), $ar 2 = FMT ($ar 2), $str = Implode (", $ar 1)." \ n ". Implode (', $ar 2); echo"{$str} ";/*//output123 ABC C1 c2 c3456 def d1 D2 d3*/
------Solution--------------------
discuss
It's not going to happen, it's in the next regular expression.
Preg_replace ('/\[.*\]/', ', $v);
Because the number of rows will be many, want to be efficient and express a simple solution!
------Solution--------------------
Not afraid of the line, the above code to modify a little to adapt to the indefinite row of data.
------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--------------------
Halo. 。 I just see the regular and I feel a big head.
PHP code
$str = ' 123[test] ABC C1 c2 c3456 def d1[t EST] 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*/