HTML 标签上的 PHP preg_match()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15308803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:01:32  来源:igfitidea点击:

PHP preg_match() on HTML tags

phphtmlpreg-match

提问by P R

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

我正在尝试使用 preg_match() 检索 to。

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.

我得到的错误是:警告:preg_match(): 未知修饰符 '(' in TrialPHP.php 第 4 行。我也在特殊字符之前包含了 \。

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

我尝试过使用DOM 解析器,它运行成功。但我必须使用正则表达式进行赋值。我该怎么办??

回答by Adam Plocher

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 />\n\n";
echo $string1[1];
?>

回答by Skydiver

You need a delimiter, for example #.

您需要一个分隔符,例如#

Try:

尝试:

$regexp='#<div class="cropped-image" style="width:102px;height:102px;" >(.*)</div>#';