php- preg_match不工作也不str_replace

时间:2017-09-08 09:53:20

标签: php regex str-replace

str_replacepreg_match无法使用以下代码。

$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';
}

1 个答案:

答案 0 :(得分: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>

之前有很多od空格