如何使用PHP将preg_match与file_get_contents一起使用?

问题描述 投票:1回答:1

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

我有一个PHP代码片段,但我只得到一个白色的空白页面。

$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;">[email protected]</strong>
    </div>';
}
php preg-match file-get-contents preg-match-all
1个回答
2
投票

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

$ 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;">[email protected]</strong>
    </div>';
}

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

© www.soinside.com 2019 - 2024. All rights reserved.