I'm looking for a way to search in a folder with .phtml files and 'scan' the files for words.

This what I have in my .phtml files (twig engine):

<span>{{ 'language'|trans }}</span>
<a href="#">{{ 'Settings'|trans }}</a>

I have multiple .phtml files with these translations in it.
Now I would like to scan all the files and search for the strings before the |trans .

Is there a way to do this? And could you give me a tip how to begin with this? Because I have no clue how to start with this.

有帮助吗?

解决方案

As said there are more ways to do this. This is just one take on it, I'm not pretending this is the best way to do it (in fact, my preferred way would be totally different, including not using PHP).

Use opendir() to create a list of files to scan. On the documentation page some examples are shown that are quite useful for your purpose. Loop through the files and get their contents using, for example, file_get_contents(). Then use preg_match_all() on that content to find the strings you want. An example regex, based on the content in question, would be /\{{2}\s*([^|]+)\|trans\s*\}{2}/i.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top