我正在尝试对保存在数组元素$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";
}
}
使用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";
}
}