duanluanhui8348 2019-02-14 09:46
浏览 45
已采纳

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

Working in WordPress (PHP). I want to set strings to the database like below. The string is translatable, so it could be in any language keeping the template codes. For the possible variations, I presented 4 strings here:

<?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%%';

To make the string human-readable, for the %%AUTHOR%% part I can change the string like below:

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

But for status and priority, I have different substrings of different lengths.

Question is:
How can I make those dynamic substring be replaced on-the-fly so that they could be human-readable like:

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

Those unsoundable words are to let you understand the nature of a translatable string, that could be anything other than known words.

I think I can proceed with something like below:

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

But how can I fill inside those conditionals efficiently?

Edit

I might not fully am clear with my question, hence updating the query. The issue is not related to array str_replace.

The issue is, the $string that I need to detect is not predefined. It would come like below:

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

Where they will be filled dynamically with values in the $status and $priority.

So when it comes to str_replace() I will actually use functions to get their appropriate labels:

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

}

The function has some missing points where I currently am stuck. :(

Can you guide me a way out?

  • 写回答

1条回答 默认 最新

  • dongliao9233 2019-02-19 21:02
    关注

    It sounds like you need to use RegEx for this solution.
    You can use the following code snippet to get the effect you want to achieve:

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

    Of course, the above code needs to be changed for STATUS and any other replacements that you require.

    Explaining the RegEx code in short it:

    • /
      The starting of any regular expression.
    • %%PRIORITY_
      Is a literal match of those characters.
    • (
      The opening of the match. This is going to be stored in the third parameter of the preg_match.
    • .
      This matches any character that isn't a new line.
    • *?
      This matches between 0 and infinite of the preceding character - in this case anything. The ? is a lazy match since the %% character will be matched by the ..

    Check out the RegEx in action: https://regex101.com/r/qztLue/1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Opencv配置出错
  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?