php - preg_match 函数不适用于 if 语句?
问题描述
我正在尝试对保存在数组元素中的字符串使用函数 pre_match $lines[$i]
。
字符串就像“关键字:南极洲;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";
}
}
推荐阅读
- javascript - Rails - 添加 .js.erb 文件时未加载 Ajax 请求
- react-native - react-native 登录屏幕 - 使用空登录字段按下登录按钮时出错
- c++ - 在符合 LLVM IR 中的 C 调用约定的同时按值传递结构
- node.js - res.cookie 没有在浏览器中设置 cookie
- android - 当 mockito 类作为依赖项传递时,Mockito 不返回 mockValue
- python - 无法在 Python 中使用多行形成正则表达式
- python - 从 wheel 安装密码学 2.2.1 或更高版本
- datatable - 数据表和 Yadcf。类型错误:oTable 未定义
- c# - C# 属性。“如果(值是双倍)”始终为真
- json - 将 JSON Plus JSON Schema 转换为原始 HTML?