我如何 str_replace 部分地 PHP 在具有未知密钥内容的动态字符串中

How can I str_replace partially in PHP in a dynamic string with unknown key content

使用 WordPress (PHP)。我想将字符串设置为数据库,如下所示。该字符串是可翻译的,因此它可以是任何保留模板代码的语言。对于可能的变化,我在这里提供了 4 个字符串:

<?php
$string = '%%AUTHOR%% changed status to %%STATUS_new%%';
$string = '%%AUTHOR%% changed status to %%STATUS_oldie%%';
$string = '%%AUTHOR%% changed priority to %%PRIORITY_high%%';
$string = '%%AUTHOR%% changed priority to %%PRIORITY_low%%';

为了使字符串易于阅读,对于 %%AUTHOR%% 部分,我可以像下面这样更改字符串:

<?php
$username = 'Illigil Liosous'; // could be any unicode string
$content = str_replace('%%AUTHOR%%', $username, $string);

但是对于状态和优先级,我有不同长度的不同子串。

问题是:
我怎样才能即时替换这些动态子字符串,以便它们可以像人类可读的那样:

Illigil Liosous changed status to Newendotobulous;
Illigil Liosous changed status to Oldisticabulous;
Illigil Liosous changed priority to Highlistacolisticosso;
Illigil Liosous changed priority to Lowisdulousiannosso;

那些读不出来的词是为了让你理解可翻译字符串的本质,它可以是已知词以外的任何东西。

我想我可以继续进行以下操作:

<?php
if( strpos($_content, '%%STATUS_') !== false ) {
    // proceed to push the translatable status string
}
if( strpos($_content, '%%PRIORITY_') !== false ) {
    // proceed to push the translatable priority string
}

但是我怎样才能有效地填充这些条件呢?

编辑

我可能不太清楚我的问题,因此更新查询。该问题与数组 str_replace.

无关

问题是,我需要检测的 $string 没有预定义。它会像下面这样:

if($status_changed) :
    $string = "%%AUTHOR%% changed status to %%STATUS_{$status}%%";
else if($priority_changed) :
    $string = "%%AUTHOR%% changed priority to %%PRIORITY_{$priority}%%";
endif;

其中将用 $status$priority 中的值动态填充它们。

所以当谈到str_replace()时,我实际上会使用函数来获取它们适当的标签:

<?php
function human_readable($codified_string, $user_id) {

    if( strpos($_content, '%%STATUS_') !== false ) {
        // need a way to get the $status extracted from the $codified_string
        // $_got_status = ???? // I don't know how.
        get_status_label($_got_status);
        // the status label replacement would take place here, I don't know how.
    }

    if( strpos($_content, '%%PRIORITY_') !== false ) {
        // need a way to get the $priority extracted from the $codified_string
        // $_got_priority = ???? // I don't know how.
        get_priority_label($_got_priority);
        // the priority label replacement would take place here, I don't know how.
    }

    // Author name replacement takes place now
    $username = get_the_username($user_id);
    $human_readable_string = str_replace('%%AUTHOR%%', $username, $codified_string);

    return $human_readable_string;

}

函数有一些遗漏点,我目前卡在了那里。 :(

你能给我指条路吗?

听起来您需要为此解决方案使用 RegEx。
您可以使用以下代码片段来获得您想要达到的效果:

preg_match('/%%PRIORITY_(.*?)%%/', $_content, $matches);
if (count($matches) > 0) {
    $human_readable_string = str_replace("%%PRIORITY_{$matches[0]}%%", $replace, $codified_string);
}

当然,上面的代码需要更改 STATUS 以及您需要的任何其他替换。

简而言之解释 RegEx 代码:

  • /
    任何正则表达式的开头。
  • %%PRIORITY_
    是这些字符的字面匹配。
  • (
    比赛开幕。这将存储在 preg_match.
  • 的第三个参数中
  • .
    这匹配任何不是新行的字符。
  • *?
    这匹配前面字符的 0 和无穷大 - 在本例中为 anything? 是惰性匹配,因为 %% 字符将与 ..
  • 匹配

查看运行中的正则表达式:https://regex101.com/r/qztLue/1