PHP Regular expression specific, really big head. How to solve it

Source: Internet
Author: User
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*/
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.