RegEx matches the href and content marked by a in the div.

Source: Internet
Author: User

RegEx matches the href and content marked by a in the div.

 

 

 

After a long time, I got into touch with regular expressions again. It seems like a similar feeling. Do you have such a feeling? I must have mastered such a small CASE, so I took the time to read the manual, so I understood it, so next time it seems similar ......

 

Write down the places used today:

// Demo

 

$str = '
  • Li
  • Running shoes and basketball shoes
Tryagin '; // first match DIV preg_match_all ('/(.*?) /I ', $ str, $ matches); if (! Empty ($ matches) {$ target_content = $ matches [0] [0];} // The a mark is matched for the second time. Note () is to return the matched content preg_match_all ('/(. *?) /I ', $ target_content, $ matches_detail); if (! Empty ($ matches_detail) {$ href_content = $ matches_detail [1]; $ detail_content = $ matches_detail [2];}
Method:

 

 

/*** Function: obtain topic HALL data ** @ param string $ topic name * @ param string $ pattern regular mode ** @ return array $ data ['href _ content']: link information array; $ data ['detail _ content']: Name Information array */private function get_topic_data ($ topic, $ pattern1, $ pattern2) {$ data = array (); if (empty ($ topic) | empty ($ pattern1) | empty ($ pattern2) {return $ data ;} // construct the theme pavilion data // obtain the overall data // obtain the page ID $ modpage_id = 0; $ modpages = $ this-> Modpag E_Model-> getAllPages (); foreach ($ modpages as $ one_page) {if ($ one_page ['system'] = $ topic) {$ modpage_id = $ one_page ['id'] ;}// obtain information $ version_data = $ this-> Modpage_Model-> getCurrentPageVersion ($ modpage_id ); $ page_info_raw = $ this-> Modpage_Model-> getVersionData ($ modpage_id, $ version_data ['version']); // generate data foreach ($ page_info_raw as $ k => $ v) {// if ($ topic = 'children ') {if ($ v ['m Odule_id '] = 1 & $ v ['desc'] = 'quick start baby') {$ parse_str = $ v ['content']; break ;}} else {// go to the background secondary navigation data if ($ v ['module _ id'] = 4) {$ parse_str = $ v ['content']; break ;}}// match the data required by the theme pavilion // clean the data $ parse_str = $ this-> clearData ($ parse_str); preg_match_all ($ pattern1, $ parse_str, $ matches); if (! Empty ($ matches) {$ target_content = $ matches [0] [0];} // Replace the host name $ target_content = str_replace ($ this-> website, TOUCH_URL, $ target_content); // clean data $ target_content = $ this-> clearData ($ target_content); preg_match_all ($ pattern2, $ target_content, $ matches_detail); if (! Empty ($ matches_detail) {$ data ['href _ content'] = $ matches_detail [1]; $ data ['detail _ content'] = $ matches_detail [2];} return $ data;}/*** function: clean data (filter line breaks and tabs in strings) ** @ param string $ str initial string ** @ return string $ str */private function clearData ($ str) {if (empty ($ str) {return '';} // clear the linefeed $ str = str_replace (, '', $ str); // clear the linefeed $ str = str_replace (,'', $ str ); // clear the tab $ str = str_replace (, '', $ str); $ str = trim ($ str); return $ str ;}

// Test Method

 

 

/*** Homepage portal **/public function index () {$ data = array (); $ result = array (); // obtain the theme pavilion information // array of the original theme pavilion data ('department '=> array ('type', 'regularization', 'regularization ')) $ topic_name_arr = array ('Sports '=> array ('Sport ','/
(.*?) /I ','/(.*?) /I '), 'womenshoes' => array ('women ','/(.*?) /I ','/(.*?) /I '), 'menshoes' => array ('Man ','/(.*?) /I ','/(.*?) /I '), 'outdoor' => array ('outdoor ','/(.*?) /I ','/(.*?) /I '), 'Children' => array ('children ','/(.*?) /I ','/(.*?) /I '),); // organization data foreach ($ topic_name_arr as $ type => $ pattern) {$ result = $ this-> get_topic_data ($ type, $ pattern [1], $ pattern [2]); $ data ['data'] [$ pattern [0] = $ result ;} $ h_data ['jspath'] = 'index'; $ this-> load-> view (touch/common/header, $ h_data ); $ this-> load-> view (touch/index, $ data); $ this-> load-> view (touch/common/footer );}

 

 

Application scenarios:

 

All men's shoes 

Casual shoes

Loading shoes

Business shoes

Sandal

Outdoor shoes

Canvas shoes

Shoes

Men's boots

Cotton shoes

Popular categories

New product in January

Fashion shoes

Oxford shoes

Men's boots

Logging shoes

Sailing shoes

Business leather shoes

High-rise shoes

Military guard boots



 

 

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.