preg_match函数不能与if语句一起使用?

问题描述 投票:0回答:1

我正在尝试对保存在数组元素$lines[$i]中的字符串使用pre_match函数。

该字符串就像“关键字:南极洲; Landsat-8; ASTER;区域地质图;北维多利亚州土地”。

尽管该字符串包含/ Keywords /,但程序始终都转到else部分,并显示“不在此处”。

任何帮助,为什么??

提前感谢

$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";
            }
        }
php find word
1个回答
1
投票

使用strpos / stripos php.net,因为它反而更快。

不需要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";
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.