2015-11-03 40 views
0

我想在讀取第一行文件時檢測一些單詞。用php中的preg_match檢測多個單詞

這是我的代碼

$open = @fopen($file,"r"); 
    $line = @fgets($open); 
    $close = @fclose($open); 
    return preg_match('/\<\?php/',$line); 

字檢測

"<?php","<?","<html>" 

我不知道如何與多個單詞做到這一點, 任何幫助,請

回答

0

http://php.net/manual/en/regexp.reference.alternation.php - 改變可以幫助你。你的具體情況,你有你的正則表達式擴展爲以下:

return preg_match('/^<(?:\?(?:php)?|html>)/',$line); 
+0

謝謝大家幫忙 –

+0

如何爲#在/ usr/bin中/ perl的還工作嗎? –

+0

對不起,我不太瞭解這個評論。你在問如何在perl中執行相同的檢查嗎?如果是的話,它會是這樣的卑鄙: 'perl -e'my $ str =「<?php」; if($ str =〜/^<(?:\?(?: php)?| html>)/){print「Matched!\ n」}' – user2655603