2

preg_match() を使用して to を取得しようとしています。

私は書いた:

<?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];
?>

入力:

<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>

私が得ているエラーは次のとおりです:Warning: preg_match(): Unknown modifier '(' in TrialPHP.php on line 4. 特殊文字の前にも \ を含めました。

私はDOM パーサーを試してみましたが、正常に実行されましたが、割り当てには正規表現を使用する必要があります。どのように進めればよいですか??

4

2 に答える 2

3

これを試して。それをテストしただけで、うまくいくように見えました:

<?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];
?>
于 2013-03-09T08:48:38.573 に答える
0

たとえば、区切り文字が必要です#

試す:

$regexp='#<div class="cropped-image" style="width:102px;height:102px;" >(.*)</div>#';
于 2013-03-09T08:40:02.170 に答える