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 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用