php - 在 PHP 代码库中查找所有字符串

标签 php string

我有几百万行的 PHP 代码库没有显示和逻辑的真正分离,我正试图提取代码中表示的所有字符串以进行本地化.显示和逻辑的分离是一个长期目标,但现在我只想能够本地化。

在代码中,字符串以 PHP 的所有可能格式表示,因此我需要一种理论(或实践)方法来解析我们的整个源代码,并且至少找到每个字符串所在的位置。当然,理想情况下,我会用函数调用替换每个字符串,例如

"this is a string"

将替换为

_("this is a string")

当然我需要同时支持单双quote format .其他的我不太关心,它们很少出现,我可以手动更改它们。

此外,我当然不想本地化数组索引。所以像这样的字符串

$arr["value"]

不应该成为

$arr[_("value")]

谁能帮我开始做这件事?

最佳答案

你可以使用 token_get_all()从 PHP 文件中获取所有标记 例如

<?php

$fileStr = file_get_contents('file.php');

foreach (token_get_all($fileStr) as $token) {
    if ($token[0] == T_CONSTANT_ENCAPSED_STRING) {
        echo "found string {$token[1]}\r\n";
        //$token[2] is line number of the string
    }
}

你可以做一个非常肮脏的检查,它没有被像这样的东西用作数组索引:

$fileLines = file('file.php');

//inside the loop and if
$line = $fileLines[$token[2] - 1];
if (false === strpos($line, "[{$token[1]}]")) {
    //not an array index
}

但你真的很难正确地做到这一点,因为有人可能写了一些你可能没有想到的东西,例如:

$str = 'string that is not immediately an array index';
doSomething($array[$str]);

编辑 正如 Ant P 所说,对于此答案的第二部分,您最好在周围的标记中寻找 [] 而不是我的 strpos hack,像这样:

$i = 0;
$tokens = token_get_all(file_get_contents('file.php'));
$num = count($tokens);
for ($i = 0; $i < $num; $i++) {
    $token = $tokens[$i];

    if ($token[0] != T_CONSTANT_ENCAPSED_STRING) {
        //not a string, ignore
        continue;
    }

    if ($tokens[$i - 1] == '[' && $tokens[$i + 1] == ']') {
        //immediately used as an array index, ignore
        continue; 
    }

    echo "found string {$token[1]}\r\n";
    //$token[2] is line number of the string
}

关于php - 在 PHP 代码库中查找所有字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/571734/

相关文章:

python - 如何根据python中其他列的单元格条件移动列的位置

C 动态字符串数组

string - 通过指定子字符串的第一个和最后一个来提取字符串中的子字符串

c# - 为什么 Regex.Split 在正则表达式上拆分时保留分隔符

php - 表单不传递数据,PHP

php - 使用自定义元日期显示自定义帖子类型,但保留 WP 中的日程安排功能

php - AJAX 帖子 : Not able to retrieve posted data array in PHP

javascript - 如何通过 Javascript 中的一元运算符将数字转换为字符串?

php - AWS上执行 "PutObject"出错,上传失败

php - 尝试进行 php/mysql 搜索,需要将字符串分解为数组并搜索