2015-11-03 112 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