About the array returned by preg_match_all-php Tutorial

Source: Internet
Author: User
I want to use a regular expression to match the regular expression in the html tag. the regular expression is #(. *) # because the original matching text is actually a regular table, I want to save the matching results to the database separately, but the returned two-dimensional array I don't understand. the result is positive... I want to use a regular expression to match html

Label
The regular expression used is #(.*)#

Because the matched original text is actually a regular table, I want to save the matching results to the database separately.

But I don't understand the returned two-dimensional array.
My understanding is that the result of the first dimension is the result of normal matching, and then the second dimension is in the result of the one dimension. but actually, the first dimension array is the same as the second dimension array. ah

I found a preg_match parameter.
Parameter description:
Parameter description
Pattern regular expression
The subject must match the retrieved object.
(Optional) matches. it stores an array of matching results. $ matches [0] will contain the text that matches the entire pattern, $ matches [1] will contain the text that matches the child pattern in the first captured bracket, and so on

(O. o and so on. you can't understand it. beginners can help everyone)

The code is as follows:


   MondayTuesdayWednesdayThursdayFridaySaturdaySundaySection 111121314151617Section 221;22Situation and Policy (6)
Week 1
Lu Zhi and Bai Wenjie of the auditorium24252627Section 3 English   Mathematics Section 4   Music   Later       "; Function trimall ($ str) // delete all spaces {$ qian = array (" "," "," \ t "," \ n ", "\ r"); $ hou = array ("", ""); return str_replace ($ qian, $ hou, $ str) ;}$ test = trimall ($ test); $ match = "#(.*)# "; Preg_match_all ($ match, $ test, $ TR); print_r ($ TR);?>

Reply content:

I want to use a regular expression to match html

Label
The regular expression used is #(.*)#

Because the matched original text is actually a regular table, I want to save the matching results to the database separately.

But I don't understand the returned two-dimensional array.
My understanding is that the result of the first dimension is the result of normal matching, and then the second dimension is in the result of the one dimension. but actually, the first dimension array is the same as the second dimension array. ah

I found a preg_match parameter.
Parameter description:
Parameter description
Pattern regular expression
The subject must match the retrieved object.
(Optional) matches. it stores an array of matching results. $ matches [0] will contain the text that matches the entire pattern, $ matches [1] will contain the text that matches the child pattern in the first captured bracket, and so on

(O. o and so on. you can't understand it. beginners can help everyone)

The code is as follows:


   MondayTuesdayWednesdayThursdayFridaySaturdaySundaySection 111121314151617Section 221;22Situation and Policy (6)
Week 1
Lu Zhi and Bai Wenjie of the auditorium24252627Section 3 English   Mathematics Section 4   Music   Later       "; Function trimall ($ str) // delete all spaces {$ qian = array (" "," "," \ t "," \ n ", "\ r"); $ hou = array ("", ""); return str_replace ($ qian, $ hou, $ str) ;}$ test = trimall ($ test); $ match = "#(.*)# "; Preg_match_all ($ match, $ test, $ TR); print_r ($ TR);?>

Have you understood this sentence?
$ Matches [1] will contain the text that matches the child pattern in the first captured bracket. what does the bracket mean?

After understanding this, I am reminding you that your $ matches [0] must have more TR tags than $ matches [1?

preg_match_allInstead of stopping a match, all results are obtained after matching. So$matches[0]It is not a string but an array of string. Then, each matching expression corresponds to a substring. Therefore, the same applies to [1.

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.