【问题标题】:php- preg_match not working nor str_replacephp-preg_match 不工作也不 str_replace
【发布时间】:2023-04-09 08:45:01
【问题描述】:

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

【问题讨论】:

  • 当然他们在页面的文本中找不到字符串。文本在那里,但它包含一些字符串中不存在的换行符。

标签: php regex str-replace


【解决方案1】:

您错过了 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>

每个换行符都有换行符。 &lt;td&gt;前面有很多od空格

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    相关资源
    最近更新 更多