当我上传文件时,想要检查文件中是否存在文本aantal
。我正在使用此代码进行检查:
<?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 />';
}
}
}
?>
文件处理没有给出任何错误,并且出现了文本Aantal
(针2224)。
上传后的输出为:
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 )
有没有建议为什么不显示针或针2224的文本?
答案 0 :(得分:1)
"#22 Aantal:2"
与"Aantal"
不同。 array_search
会查找完全匹配的内容,而不是包含您要查找的内容的内容。
preg_grep
会更好地为您服务:
$matches = preg_grep('/Aantal/', $lines);
foreach ($matches as $m) {
echo "Find " . $m;
}
答案 1 :(得分:0)
如果您想要像这样松散地搜索,可以使用preg_grep。
$match = reg_grep("/Aantal/", $lines);
这使用正则表达式并查找任何包含“ Aantal”的数组值
答案 2 :(得分:0)
即使已经回答我也尝试过
space
作为分界符将每一行爆炸成单词occurrence
的前word
到该word array
未找到或测试多次出现
<?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');
?>