0

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?

Mayeenul Islam
  • 4,532
  • 5
  • 49
  • 102
  • Can't you just use `str_replace` array search / replacements? https://3v4l.org/aHVgS – JustCarty Feb 14 '19 at 10:05
  • @JustCarty thank you for your suggestion. But actually, my case is a bit different. I've updated my question with my context. – Mayeenul Islam Feb 14 '19 at 14:01
  • 1
    RegEx? `preg_match('/%%PRIORITY_(.*?)%%/', $_content, $matches); if (count($matches) > 0) { $human_readable_string = str_replace("%%PRIORITY_{$matches[0]}%%", $replace, $codified_string); }` – JustCarty Feb 14 '19 at 14:07
  • 1
    @JustCarty in fact, I just found the same solution right now. http://prntscr.com/ml2ff2 . Telepathy? :) Thanks a lot. I requested to reopen the question. If reopened, would love to accept your answer thankfully. <3 – Mayeenul Islam Feb 14 '19 at 14:18
  • 1
    No worries, glad you have a solution! I would strongly recommend that you use the `.*?` version, not the one that is in the screenshot. This is called a [lazy match](https://www.regular-expressions.info/repeat.html). Also see this example of why you should use it: https://regex101.com/r/qztLue/1 (Try removing the `?` and see what changes!) – JustCarty Feb 14 '19 at 14:21
  • Appreciated. Thanks again. – Mayeenul Islam Feb 14 '19 at 14:28
  • @JustCarty the question is reOpened. Can you please add your comment as an answer, so that I can accept it? – Mayeenul Islam Feb 19 '19 at 20:53

1 Answers1

1

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

JustCarty
  • 3,839
  • 5
  • 31
  • 51