Php preg_match函数不适用于if语句?

Php preg_match函数不适用于if语句?,php,find,word,Php,Find,Word,我试图对保存在数组元素$lines[$I]中的字符串使用函数pre_match 字符串是这样的关键字:南极洲;陆地卫星8号;紫菀;区域地质填图;北维多利亚土地 尽管字符串包含/Keywords/,但程序始终转到else部分,并在此处显示not 有什么帮助吗?为什么 提前谢谢 $lines = file('C:\Tamer\Open Polar\New Keywords\Original citations files\combined.txt'); // Loop through our arr

我试图对保存在数组元素$lines[$I]中的字符串使用函数pre_match

字符串是这样的关键字:南极洲;陆地卫星8号;紫菀;区域地质填图;北维多利亚土地

尽管字符串包含/Keywords/,但程序始终转到else部分,并在此处显示not

有什么帮助吗?为什么

提前谢谢

$lines = file('C:\Tamer\Open Polar\New Keywords\Original citations files\combined.txt');
// Loop through our array

$length = count($lines);
for ($i = 0; $i <= $length; $i++) {

    settype($lines[$i], "string");              // Be sure that everything is string

    if(preg_match("/Keywords:/",$lines[$i]))
        {
            echo "we got it" . "<br />\n";
            }
        else
            {
            echo "not here" . "<br />\n";
            }
        }
使用strpos/stripos,因为它速度更快

Settype是不必要的,因为文件将被解析为字符串

$lines = file('C:\Tamer\Open Polar\New Keywords\Original citations files\combined.txt');

$length = count($lines);
for ($i = 0; $i < $length; $i++) 
{
    if(strpos($lines[$i], 'Keywords:') !== false)
    {
         echo "we got it" . "<br />\n";
    }
    else
    {
        echo "not here" . "<br />\n";
    }
}

//请确保所有内容都是字符串-文件中的每一行都是字符串。$我可以请您与此/这些行和周围的2-3行共享文件部分吗?我使用随机txt文件进行测试,文件包含关键字:and work..这是您的代码,我已为我更改它正在工作。在这里测试:可能检查你的文件。如果$file==false{die'文件为false';}