遇到正则难题,请高手指点

WBOY
Release: 2016-06-23 13:21:17
Original
992 people have browsed it

大家好,我正在做正则匹配,想把以下属性名称(如颜色分类和套餐类型)和属性值(白色,黑色,套餐一,套餐二,套餐三)抽取出来,我的做法是先把每个

  中间的内容通过正则匹配出来,然后用程序再把  里面的
属性名称
和 属性值匹配出来,但是第一步就测试失败:/
.*) 我用的是这个样的正则匹配每个
,但是一直弄不好,请高手指点,谢谢。


        
颜色分类

        

            
        

  

        
套餐类型

        

            
        

   


回复讨论(解决方案)

//自己到网上下一个include('simple_html_dom.php');$dom=new simple_html_dom();$dom->load($s);//$s 就是你的html$dls=$dom->find('dl');$res=array();foreach($dls as $k=>$dl){	$res[$k]['type']=$dl->find('dt')[0]->text();	foreach($dl->find('li') as $k1=>$li){		$res[$k][] = $li->find('span')[0]->text();	}}echo "<pre class="brush:php;toolbar:false">";print_r($res);echo "
Copy after login
";/*Array( [0] => Array ( [type] => 颜色分类 [0] => 白色 [1] => 黑色 ) [1] => Array ( [type] => 套餐类型 [0] => 套餐一 [1] => 套餐二 [2] => 套餐三 ))*/

$s =<<< HTML<dl class="J_Prop tb-prop tb-clear  J_Prop_Color ">        <dt class="tb-property-type">颜色分类</dt>        <dd>            <ul data-property="颜色分类" class="J_TSaleProp tb-img tb-clearfix">                <li data-value="1627207:3232483" class="tb-txt">                            <a href="javascript:void(0);">                                <span>白色</span>                            </a>                            <i>已选中</i>               </li>       <li data-value="1627207:28327" class="tb-txt">                            <a href="javascript:void(0);">                                <span>黑色</span>                            </a>                            <i>已选中</i>               </li>            </ul>        </dd></dl>  <dl class="J_Prop tb-prop tb-clear ">        <dt class="tb-property-type">套餐类型</dt>        <dd>            <ul data-property="套餐类型" class="J_TSaleProp tb-clearfix">                <li data-value="5919063:3266781">                      <a href="javascript:void(0);">                           <span>套餐一</span>                      </a>                      <i>已选中</i>                </li>                                    <li data-value="5919063:3266779">                            <a href="javascript:void(0);">                                <span>套餐二</span>                            </a>                            <i>已选中</i>                </li>                                    <li data-value="5919063:3266785">                      <a href="javascript:void(0);">                           <span>套餐三</span>                      </a>                      <i>已选中</i>                </li>            </ul>        </dd></dl>   HTML;preg_match_all('#<dl class="J_Prop tb-prop tb-clear.*?>(.+?)</dl>#is', $s, $m);foreach($m[1] as $i=>$v) {  preg_match_all('#<(dt|span).*>(.+)</\1>#isU', $v, $m);  $r[$i] = $m[2];}print_r($r);
Copy after login
Array(    [0] => Array        (            [0] => 颜色分类            [1] => 白色            [2] => 黑色        )    [1] => Array        (            [0] => 套餐类型            [1] => 套餐一            [2] => 套餐二            [3] => 套餐三        ))
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template