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

发布于 2024-11-26 14:54:36 字数 951 浏览 1 评论 0原文

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

文本文件sample.txt是这样的:

A registration has been received. 

Name: test
Lastname: test2 
Email: [email protected]
Company/School: Test2
Company2: test3
Address1: test4
Address2: test5
City: test6
State: test7
Country: test8
Zipcode: test9

现在我已经使用file()函数将该文本文件放入数组中。

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

然后我遍历循环以提取每个值并从该数组中搜索单词“Name”。

    $data1='Name';

    foreach($file_array as $value){
    if(stripos($value,$data1)===FALSE)
        echo "Not Found";
     else 
       echo "Found";
    }

但它总是打印“未找到”。我尝试过使用 strpos、strstr、preg_match 但无济于事。另外,如果我使用普通的单词数组而不是从文件创建,它也可以正常工作。

提前致谢。

更新:我在这个问题中的目标是首先检测它是哪个字段。 “名称”,然后是它的值,例如。 '测试'

I have been trying to extract specific words from an array which was created from a text file using file() function in php.

The text file sample.txt is like this:

A registration has been received. 

Name: test
Lastname: test2 
Email: [email protected]
Company/School: Test2
Company2: test3
Address1: test4
Address2: test5
City: test6
State: test7
Country: test8
Zipcode: test9

Now I have used file() function to put this text file into an array.

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

Then I traversed through loop to extract each value and search for word say 'Name' from this array.

    $data1='Name';

    foreach($file_array as $value){
    if(stripos($value,$data1)===FALSE)
        echo "Not Found";
     else 
       echo "Found";
    }

But it always prints 'Not Found'. I have tried using strpos,strstr, preg_match but to no avail. Also if I use a normal array of words instead of creating from file, it works properly.

Thanks in Advance.

Update: My objective in this problem is to first detect what field it is ex. 'Name' and then its value ex. 'test'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满意归宿 2024-12-03 14:54:36

这当然可能是行结束问题或文件编码问题,我也不确定 file() 如何处理空格。

作为如何改进代码的建议,如果您根据数据创建自己的键控数组,则可以使用更可靠的 array_key_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_key_exists,如下所示:

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'];
}

It certainly could be a line ending problem or an issue with your file encoding, I'm not sure exactly how file() handles whitespace either.

As a suggestion on how to improve the code, if you create yourself a keyed array from the data then you can use the more reliable array_key_exists() function to search for your fields.

$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]);
}

Now you can use array_key_exists as follows:

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'];
}
墨小墨 2024-12-03 14:54:36

很可能您的数组中每个“行”的末尾仍然有换行符。尝试像这样加载它:

$file_array=file($file_name, FILE_IGNORE_NEW_LINES);

most likely you still have newline characters at the end of each "line" in your array. Try loading it like this:

$file_array=file($file_name, FILE_IGNORE_NEW_LINES);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文