About the array returned by Preg_match_all

Source: Internet
Author: User
I want to use regular expressions to match the HTML

In the label
The regular expression used is #(.*)#

Because the original text of the match is actually a regular form. I want to put a number of matching results into the database separately

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

I found a preg_match parameter
Parameter description:
Parameter description
Pattern Regular Expression
Subject needs to match the retrieved object
Matches optional, stores an array of matching results, $matches [0] will contain text that matches the entire pattern, $matches [1] will contain the text that matches the sub-pattern in the first captured parenthesis, and so on

(o.o analogy your sister Ah can not understand AH beginners let everyone)

The code is as follows


  
    星期一星期二星期三星期四星期五星期六星期日第一大节11121314151617第二大节21;22形势与政策(6)
第10周
吕志和礼堂 白文杰24252627第三大节 英语 数学 第四大节 音乐 晚 上 ";function trimall($str)//删除全部空格{ $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 regular expressions to match the HTML

In the label
The regular expression used is #(.*)#

Because the original text of the match is actually a regular form. I want to put a number of matching results into the database separately

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

I found a preg_match parameter
Parameter description:
Parameter description
Pattern Regular Expression
Subject needs to match the retrieved object
Matches optional, stores an array of matching results, $matches [0] will contain text that matches the entire pattern, $matches [1] will contain the text that matches the sub-pattern in the first captured parenthesis, and so on

(o.o analogy your sister Ah can not understand AH beginners let everyone)

The code is as follows


  
    星期一星期二星期三星期四星期五星期六星期日第一大节11121314151617第二大节21;22形势与政策(6)
第10周
吕志和礼堂 白文杰24252627第三大节 英语 数学 第四大节 音乐 晚 上 ";function trimall($str)//删除全部空格{ $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);?>

Excuse me, do you understand this sentence?
$matches [1] will contain text that matches the sub-pattern in the first captured parenthesis, what is the parenthesis?

After understanding, I am reminded that your $matches[0] must be more than $matches[1 "TR this label it?"

preg_match_allIt is not a match to stop, but a match to get all the results. So $matches[0] it is not a string but an array of string. Then each match gets an expression that corresponds to a substring, so [1] is the same.

  • Related Article

    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.