doufen5175 2013-03-09 08:34
浏览 43
已采纳

HTML标签上的PHP preg_match()[关闭]

I am trying to retrieve the to using preg_match().

I've written:

<?php
$file=file_get_contents('Temp.txt');
$regexp='<div class\=\"cropped-image\" style\=\"width:102px;height:102px;\">(.*)  
</div>';
preg_match($regexp,$file,$string1);
echo $string1[0];
?>

Input:

<table class="search-results" data-search-total="5068" data-search-type="artists" data-search-term="taylor" data-search-genre="all">
        <tr class="search-result artist">
<td>

    <div class="image">    
        <div class="thumbnail sm artist">
            <a href="/artist/taylor-swift-mn0000472102" data-tooltip="{&quot;id&quot;:&quot;MN0000472102&quot;,&quot;thumbnail&quot;:true,&quot;position&quot;:{&quot;my&quot;:&quot;left center&quot;,&quot;at&quot;:&quot;middle right&quot;}}">
                                <div class="cropped-image" style="width:102px;height:102px;" ><img src="http://cps-static.rovicorp.com/3/JPG_170/MI0003/436/MI0003436897.jpg?partner=allrovi.com" style="left:-25px" width="153" height="102" alt="Taylor Swift" data-debug="170x113 (63)"></div>                                </a>
        </div>
    </div>
    <div class="right-of-image">
         <div class="type">
            <span class="sprite2 icon-search-artist-new" title="artist"></span>
        </div>
        <div class="name">
            <a href="http://www.allmusic.com/artist/taylor-swift-mn0000472102" data-tooltip="{&quot;id&quot;:&quot;MN0000472102&quot;,&quot;thumbnail&quot;:true,&quot;position&quot;:{&quot;my&quot;:&quot;left center&quot;,&quot;at&quot;:&quot;middle right&quot;}}">Taylor Swift</a>            </div>

        <div class="info">
                                Country, Pop/Rock                            
            <br/>

                                2000s - 2010s                
        </div>

    </div>

</td>

The error I'm getting is:Warning: preg_match(): Unknown modifier '(' in TrialPHP.php on line 4. I have included \ before special characters too.

I have tried with DOM parser and it runs successfully.But I have to use regex for an assignment. How should I proceed??

  • 写回答

2条回答 默认 最新

  • dongling2038 2013-03-09 08:48
    关注

    Try this. Just tested it and it seemed to work:

    <?php
    $file=file_get_contents('test.txt');
    
    $regexp='/\<div class\=\"cropped-image\" style\=\"width:102px;height:102px;\" \>(.*?)\<\/div\>/';
    preg_match($regexp,$file,$string1);
    
    //print_r($string1);
    //echo "<hr />
    
    ";
    echo $string1[1];
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM