简体   繁体   English

在数组中找不到任何东西

[英]Finding in array is not finding anything

When I upload an file and would like to check if in the file the text aantal is present. 当我上传文件并想检查文件中是否存在文本aantal I am using this code to check: 我正在使用此代码进行检查:

<?php
// Loop through each file
for( $i=0 ; $i < $total ; $i++ )
{
    // Get the temp file path
    $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

    // Make sure we have a file path
    if ($tmpFilePath != "")
    {
        //Setup our new file path
        $newFilePath = $config['temp_uploads'].'/'.$_FILES['upload']['name'][$i];

        // Upload the file into the temp dir
        if(move_uploaded_file($tmpFilePath, $newFilePath))
        {
            //Handle other code here
            echo 'Upload succes<br /><br />';
            echo 'File name: '.$_FILES['upload']['name'][$i];
            echo '<br /><br />';

            $lines = file($newFilePath); //file in to an array

            echo 'Find: '.array_search('Aantal', $lines);
            echo '<br /><br />';

            print_r($lines);
            echo '<br /><br />';

            // remove file
            unlink($newFilePath);
        }
        else
        {
            echo 'Upload error<br /><br />';
        }
    }
}
?>

The filehandeling is not giving any errors and the text Aantal is present (needle 2224). 文件处理没有给出任何错误,并且出现了文本Aantal (针2224)。 The output after uploading is: 上传后的输出为:

    Upload succes

    File name: BP 01.dxf

    Find: 

    Array ( [0] => 0 [1] => SECTION [2] => 2 [3] => HEADER [4] => 9 [5] => $ACADVER [6] => 1 [7] => AC1027 [8] => 9 [9] => $ACADMAINTVER [10] =>

..... // skipping these lines because of Stackoverflow limits

=> 1 [2223] => #22 Aantal:2 [2224] => 7 [2225] => BEMATING [2226] =>

    ..... // skipping these lines because of Stackoverflow limits

    => CELLSTYLE_END [12132] => 0 [12133] => ENDSEC [12134] => 0 [12135] => EOF ) 

Any suggestions why the needle or the text of needle 2224 is not shown? 为什么没有显示针或针2224的文字的任何建议?

"#22 Aantal:2" is different from "Aantal" . "#22 Aantal:2""Aantal"不同。 array_search looks for an exact match, not something that contains what you're looking for. array_search寻找完全匹配的内容,而不是包含您要寻找的内容的内容。

You would be better served by preg_grep : preg_grep将更好地为您服务:

$matches = preg_grep('/Aantal/', $lines);
foreach ($matches as $m) {
    echo "Find " . $m;
}

If you want to search loosely like that you can use preg_grep. 如果您想要这样松散地搜索,可以使用preg_grep。

$match = reg_grep("/Aantal/", $lines);

This uses regex and finds any array values containing "Aantal" 这使用正则表达式并查找包含“ Aantal”的任何数组值

even already answered I have given a try 即使已经回答我也尝试过

  • it reads each line in the file into array 它将文件中的每一行读入数组
  • then explode each line into word using space as dilimiter 然后使用space作为分隔符将每一行分解成单词
  • then search for first occurrence of desired word into that word array 然后在该word array搜索所需word首次occurrence
  • In Image output counter differs because of loop start from zero 图像输出计数器中的值不同,因为循环从零开始
  • Not Finding or testing multiple occurrence 找不到或测试多次出现

     <?php // Loop through each file function find_word($word){ $rows = file('test.txt'); foreach($rows as $row_no => $row_content){ $words = explode(' ',$row_content); if(array_search($word,$words)){ echo $row_no ." \\t\\t\\t\\t " .$row_content; echo '<hr />'; }else{ echo 'NOT FOUND<hr />'; } } } echo find_word('Aantal'); ?> 

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM