从目录中获取文件名并在php代码中搜索这些文件名

时间:2013-11-20 20:55:08

标签: php search optimization

我有超过500个php文件,需要找到有php文件的地方。 所以我需要一个脚本或其他东西来帮助我在文件中找到这些用法/提及。 我想写一个bash脚本来做到这一点,但我不确定这是一个好主意。 你有什么想法吗?

3 个答案:

答案 0 :(得分:1)

在命令行上使用grep

grep -rnH --color=auto 'require "test.php"' SOURCE_FOLDER

该命令将打印require "test.php"的每个出现以及文件名和行号(彩色)

答案 1 :(得分:0)

您可以使用get_included_files()auto_append_file的输出附加到文件中附加脚本

答案 2 :(得分:0)

这就是我最后所做的。这个bash脚本一般解决了我的问题。关于带有空格的文件名仍有问题。如果文件名中包含空格,我会收到错误。现在对我来说没问题。

 #!/bin/bash

DIR="/path/to/dir"

# save and change IFS 
OLDIFS=$IFS
IFS=$'\n'

# read all file name into an array
FILES=$(find $DIR -type f -name "*.php")
DFILES=$FILES

#General report file
ofile="general-report.txt"

#Unused files report file
nuffile="unused-files-report.txt"

#count unused files
yok=0

echo "Files have found. Proccessing..."

#Title for general report
echo "GENERAL REPORT FOR X " >> "$ofile"

#Title for unused files report
echo "UNUSED FILES FOR X" >> "$nuffile"

for i in $FILES 
do
    bulundu=0
    dosyaadi=$(basename $i)
    echo "###########################################################################################">> "$ofile"
    echo "###########################################################################################"
    echo "------>>>>> Looking for $dosyaadi ..."
    echo "------>>>>> Looking for $dosyaadi ..." >> "$ofile"
    for j in $DFILES 
    do
        if grep -rnH --color=auto "$dosyaadi" "$j"
            then
            grep -rnH --color=auto "$dosyaadi" "$j" >> "$ofile"
            bulundu=$((bulundu+1))
        fi
    done
    if [ $bulundu -eq 0 ]
        then 
        yok=$((yok+1))
        echo "DOSYA $yok -->> $dosyaadi dosyasi hic bir php kodunda bulunamadi. Kullanilmiyor olmasi muhtemel.">> "$nuffile"
        echo "Dosyanin konumu: $i">> "$nuffile"
        echo -e '\n\n'>> "$nuffile"
    fi
    echo "__________________________________________________________________________________________">> "$ofile"
    echo "__________________________________________________________________________________________"
    echo "$dosyaadi dosyasinin diger dosyalarda $bulundu sayida eslesme bulundu.">> "$ofile"
    echo "$dosyaadi dosyasinin diger dosyalarda $bulundu sayida eslesme bulundu."
    echo "__________________________________________________________________________________________">> "$ofile"
    echo "__________________________________________________________________________________________"
    echo -e '\n\n\n\n'>> "$ofile"
    echo -e '\n\n\n\n'
done
echo "TOPLAM $yok tane dosya kullanilmiyor gorunuyor!">> "$nuffile"
echo "Heyyo! Tüm tarama bitti $ofile dosyasına yazıldı."
# restore it 
IFS=$OLDIFS