2017-09-08 5 views
-3

str_replaceおよびpreg_matchは次のコードでは機能しません。php-preg_matchが動作していないかstr_replace

$url = 'http://www.ima-appweb.com/hub/emailers/2017-07/website-emailer-2/'; 

$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n")); 
//Basically adding headers to the request 
$context = stream_context_create($opts); 
$html = file_get_contents($url,false,$context,0,2700); 
$html = htmlspecialchars($html); 
echo $html; 

if(preg_match_all('#<td colspan="3"> Corp off: I – 128, 2nd Floor, Mohammadpur, Bikaji, New Delhi 110066. <br> © Copyright 2014 by Ima Appweb. All Rights Reserved. </td>#', $html)){ 
    $outputs = str_replace('<td colspan="3"> Corp off: I – 128, 2nd Floor, Mohammadpur, Bikaji, New Delhi 110066. <br> © Copyright 2014 by Ima Appweb. All Rights Reserved. </td>','<td colspan="3"><strong> Address: '.$address.', '.$city.', '.$country.'<br/>'.$website.'<br/>© Copyright By sunny '.$company.'</strong></td>',$html); 
} else { 
    echo 'didnot match'; 
} 
+0

もちろん、ページのテキストに文字列が見つかりません。テキストはそこにありますが、あなたの文字列にはないいくつかの改行が含まれています。 – axiac

答えて

0

htmlコードに改行やスペースがありません。それを見てください

   <td colspan="3"> 
        Corp off: I – 128, 2nd Floor, Mohammadpur, Bikaji, New Delhi 110066. 
<br> 
© Copyright 2014 by Ima Appweb. All Rights Reserved. 
         </td> 

各改行に改行記号があります。前にたくさんの空白があります<td>