Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP无法在从文件创建的数组中搜索特定单词_Php_Arrays_File_Text_Strpos - Fatal编程技术网

PHP无法在从文件创建的数组中搜索特定单词

PHP无法在从文件创建的数组中搜索特定单词,php,arrays,file,text,strpos,Php,Arrays,File,Text,Strpos,我一直在尝试从一个数组中提取特定的单词,该数组是使用php中的file()函数从文本文件中创建的 文本文件sample.txt如下所示: A registration has been received. Name: test Lastname: test2 Email: test@test.com Company/School: Test2 Company2: test3 Address1: test4 Address2: test5 City: test6 State: test7 C

我一直在尝试从一个数组中提取特定的单词,该数组是使用php中的file()函数从文本文件中创建的

文本文件sample.txt如下所示:

A registration has been received. 

Name: test
Lastname: test2 
Email: test@test.com
Company/School: Test2
Company2: test3
Address1: test4
Address2: test5
City: test6
State: test7
Country: test8
Zipcode: test9
$file_array=file($file_name, FILE_IGNORE_NEW_LINES);
现在我已经使用file()函数将这个文本文件放入一个数组中

$file_name='sample.txt';
$file_array=file($file_name);
然后,我遍历循环以提取每个值,并从这个数组中搜索单词say'Name'

    $data1='Name';

    foreach($file_array as $value){
    if(stripos($value,$data1)===FALSE)
        echo "Not Found";
     else 
       echo "Found";
    }
但它总是打印“未找到”。我试过使用strpos、strstr、preg_match,但都没有用。另外,如果我使用一个普通的单词数组而不是从文件中创建,它也可以正常工作

提前谢谢


更新:我在这个问题中的目标是首先检测它是什么字段,例如“Name”,然后检测它的值,例如“test”

最有可能的是,数组中的每一行末尾都有换行符。试着像这样加载它:

A registration has been received. 

Name: test
Lastname: test2 
Email: test@test.com
Company/School: Test2
Company2: test3
Address1: test4
Address2: test5
City: test6
State: test7
Country: test8
Zipcode: test9
$file_array=file($file_name, FILE_IGNORE_NEW_LINES);

最有可能的情况是,数组中每个“行”的末尾仍有换行符。试着像这样加载它:

A registration has been received. 

Name: test
Lastname: test2 
Email: test@test.com
Company/School: Test2
Company2: test3
Address1: test4
Address2: test5
City: test6
State: test7
Country: test8
Zipcode: test9
$file_array=file($file_name, FILE_IGNORE_NEW_LINES);

这当然可能是一个行尾问题或文件编码问题,我也不确定file()如何处理空白

作为如何改进代码的建议,如果您从数据中创建一个键控数组,那么您可以使用更可靠的array\u key\u exists()函数来搜索字段

$file_name = 'sample.txt';
$file_data = file($file_name);

// loop through each line
foreach ($file_data as $line) {

// from each line, create a two item array
$line_items = explode(":", $line);

// build an associative (keyed) array
// stripping whitespace and using lowercase for keys
$file_array[trim(strtolower($line_items[0]))] = trim($line_items[1]);
}
现在您可以使用array\u key\u,如下所示:

if (array_key_exists("name", $file_array) === false) {
  print "Not found.";
} else {
  print "Found.";  
  // and now it's simple to get that value
  print "<br />Value of name:  " . $file_array['name'];
}
如果(数组\u键\u存在(“名称”,$file\u数组)==false){
打印“未找到。”;
}否则{
打印“已找到。”;
//现在很容易得到这个值
打印“
名称的值:”.$file_数组['name']; }
当然可能是行尾问题或文件编码问题,我也不确定file()如何处理空白

作为如何改进代码的建议,如果您从数据中创建一个键控数组,那么您可以使用更可靠的array\u key\u exists()函数来搜索字段

$file_name = 'sample.txt';
$file_data = file($file_name);

// loop through each line
foreach ($file_data as $line) {

// from each line, create a two item array
$line_items = explode(":", $line);

// build an associative (keyed) array
// stripping whitespace and using lowercase for keys
$file_array[trim(strtolower($line_items[0]))] = trim($line_items[1]);
}
现在您可以使用array\u key\u,如下所示:

if (array_key_exists("name", $file_array) === false) {
  print "Not found.";
} else {
  print "Found.";  
  // and now it's simple to get that value
  print "<br />Value of name:  " . $file_array['name'];
}
如果(数组\u键\u存在(“名称”,$file\u数组)==false){
打印“未找到。”;
}否则{
打印“已找到。”;
//现在很容易得到这个值
打印“
名称的值:”.$file_数组['name']; }
基本上这个-->数组(13){[0]=>string(70)“已收到注册。”[1]=>string(1)”“[2]=>string(21)“Name:test”[3]=>string(33)“Lastname:test2”……contd.}string(21)表示“Name:test”?输入文件是否为UTF-16格式?您需要转换为UTF-8或使用mb_stripos(多字节感知)字符串功能。尝试了mb_stripos,但再次失败。事实上,我以前也尝试过用$value=utf8\u encode($value)将每个值转换为utf-8,但它也不起作用。我尝试过像mb\u stripos($value,$data1)这样的内部编码的mb\u stripos,还特别提到了像mb\u stripos($value,$data1,“utf-8”)这样的utf-8。正确吗?是的,但是utf8_encode()是用于iso8859-1->utf8的,您从utf-16开始。请改为尝试mb_convert_encoding()。基本上,这个-->数组(13){[0]=>string(70)“已收到注册。”[1]=>string(1)”“[2]=>string(21)“Name:test”[3]=>string(33)“Lastname:test2”……contd}string(21)表示“Name:test”?输入文件是否为UTF-16格式?您需要转换为UTF-8或使用mb_stripos(多字节感知)字符串功能。尝试了mb_stripos,但再次失败。事实上,我以前也尝试过用$value=utf8\u encode($value)将每个值转换为utf-8,但它也不起作用。我尝试过像mb\u stripos($value,$data1)这样的内部编码的mb\u stripos,还特别提到了像mb\u stripos($value,$data1,“utf-8”)这样的utf-8。正确吗?是的,但是utf8_encode()是用于iso8859-1->utf8的,您从utf-16开始。试试mb_convert_encoding()。我会尝试使用print_r($file_数组)并确定密钥的外观。我明白你的意思。所以键实际上是这样的数字“[2]=>Name:test”。因此,我可以通过指定一个数字来获取名称,在本例中,它是“2”而不是“Name”。但在本例中,我必须先查看数组键,然后相应地更改值,这样,如果“Name”在任何文本文件中的位置发生更改,我的脚本将中断。但无论如何,我还是要感谢你,因为它现在可以工作了。这里我并不是在搜索字符串“Name”,而是在不检查它是否包含“Name”字段的情况下查看它的位置并提取它的值。好吧,我想提醒你的是,数组看起来是这样的:Name=>test,lastname=>test2,email=>test@test.com,等等。这样,冒号左边的每个字符串都成为数组中的一个键,因此,可以使用array_key_exists()进行搜索。我会尝试使用print_r($file_数组)并确定键的外观。我明白你的意思。所以键实际上是这样的数字“[2]=>Name:test”。因此,我可以通过指定一个数字来获取名称,在本例中,它是“2”而不是“Name”。但在本例中,我必须先查看数组键,然后相应地更改值,这样,如果“Name”在任何文本文件中的位置发生更改,我的脚本将中断。但无论如何,我还是要感谢你,因为它现在可以工作了。这里我并不是在搜索字符串“Name”,而是在不检查它是否包含“Name”字段的情况下查看它的位置并提取它的值。好吧,我想提醒你的是,数组看起来是这样的:Name=>test,lastname=>test2,email=>test@test.com,等等。这样,冒号左侧的每个字符串都成为数组中的一个键,因此,可以使用array_key_exists()进行搜索。