legibe
legibe

Reputation: 592

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

I have created my own l($text) function in php for a multi lingual website. i use it like this in my documents :

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

My question is, with a php script, how can i scan all my .php files to catch all this function usages and list all the arguments used into a mysql table? the goal, of course, is to not forget any sentences in my traduction files.

I didn't find anything on google or here, so if you have any ideas, or need some more information.

Upvotes: 0

Views: 366

Answers (2)

legibe
legibe

Reputation: 592

i just finished, your help was usefull ! :-)

here is my ugly code for those who can be interested. it's not beautifuly coded, but not made to be loaded 10000 times per day so...

<?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);
    }
}
?>

small precision : i do have to escape various cases as : -> CSS : background: url('image.jpg'); and -> jQuery : $(this).html('bla bla'); so here is why the regex starts with [^a-z(] :-)

it works very well now! just have to finish later with recording entries in a mysql table and ensure that i can load the script from time to time when there are changes on the site... keep the existing translation, overwrite the existing files etc... no problem with that.

thanks a gain, this website is really helpful ! :-)

Upvotes: 0

Andrew Hall
Andrew Hall

Reputation: 3073

Could you:

  • read all *.php files with glob()
  • then use a regex to pull the strings out (preg_match())
  • strings simple mysql insert?

Seems simple enough?

Upvotes: 1

Related Questions