Php regular expressions are really big. How can this problem be solved?

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

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.