簡體   English   中英

如何在我所有的php文件中掃描自定義函數的所有用法?

[英]how to scan all usages of a custom function in all my php files?

我已經在php中為多語言網站創建了自己的l($text)函數。 我在我的文檔中這樣使用它:

echo '<h1>' . l('Title of the page') . '</h1';
echo '<p>' . l('Some text here...') . '</p>';

我的問題是,使用php腳本,我如何掃描我的所有.php文件以捕獲所有此函數的用法並將所有使用的參數列出到mysql表中? 當然,目標是不要忘記我的翻譯文件中的任何句子。

我沒有在Google或這里找到任何東西,因此,如果您有任何想法,或需要更多信息。

您可以...嗎:

  • 使用glob()讀取所有* .php文件
  • 然后使用正則表達式將字符串拉出(preg_match())
  • 字符串簡單的mysql插入?

看起來足夠簡單?

我剛說完,您的幫助很有用! :-)

這是我對那些可能感興趣的人的丑陋代碼。 它不是經過精美編碼的,但每天的加載次數不能達到10000次,因此...

<?php
// define a plain text document to see what appen on test
header('Content-Type: text/plain; charset=UTF-8');

$dossier = 'pages/'; // folder to scan
$array_exclude = array('.', '..', '.DS_Store'); // system files to exclude
$array_sentences_list = array();

if(is_dir($dossier)) // verify if is a folder
{
    if($dh = opendir($dossier)) // open folder
    {
        while(($file = readdir($dh)) !== false) // scan all files in the folder
        {
            if(!in_array($file, $array_exclude)) // exclude system files previously listed in array
            {
                echo "\n".'######## ' . strtoupper($file) . ' ##########'."\n";
                $file1 = file('pages/'.$file); // path to the current file
                foreach($file1 AS $fileline)
                {
                    // regex : not start with a to z characters or a (
                    // then catch sentences into l(' and ')
                    // and put results in a $matchs array
                    preg_match_all("#[^a-z\(]l\('(.+)'\)#U", $fileline, $matchs);

                    // fetch the associative array
                    foreach($matchs AS $match_this)
                    {
                        foreach($match_this AS $line)
                        {
                            // technique of "I do not want to break my head"
                            if(substr($line, 0, 3) != "l('" AND substr($line, 0, 4) != " l('" AND substr($line, 0, 4) != ".l('")
                            {
                                // check if the sentence is not already listed
                                if(!in_array($line, $array_sentences_list))
                                {
                                    // if not, add it to the sentences list array and write it for fun !
                                    $array_sentences_list[] = $line;
                                    echo $line . "\n";
                                }
                            }
                        }
                    }
                }
            }
        }
        closedir($dh);
    }
}
?>

小精度:我確實不得不逃避各種情況,例如:-> CSS:background:url('image.jpg'); 和-> jQuery:$(this).html('bla bla'); 所以這就是為什么正則表達式以[^ az(] :-)開頭的原因

現在效果很好! 只需稍后在mysql表中記錄條目即可完成操作,並確保當站點發生更改時我可以不時加載腳本...保留現有翻譯,覆蓋現有文件等...沒問題那。

謝謝,這個網站真的很有幫助! :-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM