【问题标题】:How to use preg_match with file_get_contents using PHP?如何使用 PHP 将 preg_match 与 file_get_contents 一起使用?
【发布时间】:2016-07-25 09:06:55
【问题描述】:

如何正确使用 preg_match 和 file_get_content?我有一个用户可以登录的表单,如果用户输入了错误的信息,file_get_contents 中的默认回显将使用 preg_match 替换为新消息,但它不起作用。

我有一个 PHP sn-p,但我只得到一个白色的空白页。

$reply = file_get_contents($url, false, $context);
if(preg_match($reply, 'No information was found'){
    echo("<script>location.href = '/page/message/';</script>");
} else {
    echo $reply;   
    echo '<div>
   For any other questions please contact us Monday - Sunday 9am - 6pm at <strong style="color: #000;">1235456</strong> or <strong style="color: #000;">info@example.com</strong>
    </div>';
}

【问题讨论】:

    标签: php preg-match file-get-contents preg-match-all


    【解决方案1】:

    对于模式,您必须使用分隔符使用

    $reply = file_get_contents($url, false, $context);

    if(preg_match('/No information was found/', $reply)){
        echo("<script>location.href = '/page/message/';</script>");
    } else {
        echo $reply;   
        echo '<div>
       For any other questions please contact us Monday - Sunday 9am - 6pm at <strong style="color: #000;">1235456</strong> or <strong style="color: #000;">info@example.com</strong>
        </div>';
    }
    

    添加/ 分隔符和preg_match 第一个参数是您在第二个参数中使用的模式

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 2019-01-30
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多