【发布时间】:2018-01-24 14:57:29
【问题描述】:
我有一个配置文件,我在其中存储所有字符串以便于参考和简化翻译 - 以防万一我需要将我的应用程序部署到我们的某个国际位置。
我想从 HTML 和 PHP 代码中删除我的硬编码字符串。
This site 表明 IntelliJ IDEA 能够突出显示硬编码的字符串。但是,我在我的 PhpStorm 2017.2 安装中没有看到这样的选项。
这是我想清除并用非硬编码字符串替换的 HTML 示例:
<thead>
<tr>
<th class="text-center">#</th>
<th>Benutzername</th>
<th>Nachname</th>
<th>Vorname</th>
<th>E-Mail</th>
<th>Rollen</th>
<th></th>
</tr>
</thead>
但是,PhpStorm 不会突出显示硬编码的字符串或建议任何修复。 PHP 代码也是如此。
如果有任何兴趣,以下是我通常从我的配置数组中检索字符串的方式。
/**
* Extract a string from the config file depending on its tag.
*
* @param string $tag The string to extract.
*
* @return mixed The string that was extracted. At least it should usually be a string, but it could also be
* something different, depending on the config.
*/
function get_string($tag)
{
// Check the config file first.
global $config;
$string = $config['strings'][$tag];
if (!isset($string)) {
// The faulty tag can be found in the logfile.
logfile("String Tag not found: " . $tag);
$string = $tag;
}
return $string;
}
我只是将数组索引赋予函数并从配置数组中检索字符串。如果我需要国际化,我可以修改这个函数。
【问题讨论】:
-
HTML 是文本,由元素和文本片段组成是正常的。恕我直言,HTML 中“硬编码字符串”的想法不合适。 PHP 还处理很多不是“硬编码字符串”的字符串。数组键(包括
$_GET[]、$_POST[]等中从前端接收的值)是字符串,文件名(include/require等)是字符串。甚至在 PHP 脚本中定义哪些是“硬编码字符串”和哪些是合法字符串也不是一件容易的事。 -
嗯,我没想到。你绝对是对的。
-
您可以使用“在文件中查找”命令在 PHP 脚本中查找字符串(搜索
['"]并检查“RegExp”),但您可能会被大量的字符串,其中许多是数组键或其他字符串(分隔符、SQL 查询),它们是代码的一部分,而不是输出的一部分。 -
如果您已经得出结论,请继续提供答案。编写它可以帮助您为如何继续制定更好的计划,并且可能会帮助遇到相同问题的其他人。
-
我也很想知道作为 PHPStorm 用户为什么这个功能对我不可用编辑:显然这是一个 Intellij IDEA 唯一的功能 w/Java?
标签: php html string intellij-idea phpstorm