2016-07-25 204 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第一個參數是你在第二個參數使用模式模式