2016-07-25 208 views
1

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

我有一个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>'; 
} 

回答

2

对于模式,你必须使用一个分隔符使用

$回复=的file_get_contents($网址,虚假,$背景);

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第一个参数是你在第二个参数使用模式模式