For more information about regular expressions, see The php Tutorial.

Source: Internet
Author: User
If you encounter a regular expression problem, please kindly advise. I am doing regular expression matching and want to set the following attribute names (such as color classification and package type) and attribute values (white, black, package 1, package 2 and package 3 ).
The content in the middle is matched by regular expressions, and then
Attribute name
It matches the property value, but the test fails in the first step :/
. *) <\/Dl> I use this regular expression to match each
But it has never been easy. please give me some advice. thank you.


Color classification





  • White

    Selected



  • Black

    Selected





Package Type





  • Package 1

    Selected



  • Package 2

    Selected



  • Package 3

    Selected




Reply to discussion (solution)

// Add an include ('simple _ html_dom.php ') on the Internet by yourself; $ dom = new simple_html_dom (); $ dom-> load ($ s ); // $ s is your html $ dls = $ dom-> find ('dl '); $ res = array (); foreach ($ dls as $ k => $ dl) {$ res [$ k] ['type'] = $ dl-> find ('dt ') [0]-> text (); foreach ($ dl-> find ('Lil') as $ k1 => $ li) {$ res [$ k] [] = $ li-> find ('span ') [0]-> text () ;}} echo"
";print_r($res);echo "
";/* Array ([0] => Array ([type] => color classification [0] => White [1] => Black) [1] => Array ([type] => package type [0] => Package 1 [1] => package 2 [2] => package 3 ))*/

$ S = <HTML
  
  
Color classification
  • WhiteSelected
  • BlackSelected
Package Type
  • Package 1Selected
  • Package 2Selected
  • Package 3Selected
HTML; preg_match_all ('#
Array ([0] => Array ([0] => color classification [1] => White [2] => Black) [1] => Array ([0] => package type [1] => Package 1 [2] => package 2 [3] => package 3 ))

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.