0

I am trying to replace my custom short codes, but function str_replace return empty.

I have a fixed list of search.

$search = array(15) {
  [0]=>
  string(13) "{{user_name}}"
  [1]=>
  string(17) "{{ticket_number}}"
  [2]=>
  string(15) "{{ticket_link}}"
  [3]=>
  string(18) "{{ticket_subject}}"
  [4]=>
  string(22) "{{ticket_description}}"
}

$replace = array(1) {
  [0]=>
  string(23) "ticket with attachments"
}


$subject = string(20) "{{ticket_subject}}"

echo str_replace($search, $replace, trim($subject));

Why the result is empty?

4
  • 2
    php docs said: If replace has fewer values than search, then an empty string is used for the rest of replacement values. both arrays need the same size and order. Commented Aug 18, 2021 at 10:02
  • 2
    That's invalid PHP code. Make $replace a string if you want all elements from $search replaced with the same string Commented Aug 18, 2021 at 10:03
  • If I can to solve my example of code in another way? Commented Aug 18, 2021 at 10:07
  • 1
    both arrays need the same size and order...should give you enough information to solve it. Or see brombeer's answer which already has an alternative approach. Commented Aug 18, 2021 at 10:08

2 Answers 2

2

Make $replace a string if you want all elements from $search replaced with the same string.

From the documentation: "If search is an array and replace is a string, then this replacement string is used for every value of search."

<?php
$search = [
    "{{user_name}}",
    "{{ticket_number}}",
    "{{ticket_link}}",
    "{{ticket_subject}}",
    "{{ticket_description}}",
];
$replace = "ticket with attachments";
$subject = "{{ticket_subject}}";
echo str_replace($search, $replace, $subject);

Edit: different approach using array:

<?php
$replacements = [
    "{{user_name}}" => 'Roman',
    "{{ticket_number}}" => 1234567890,
    "{{ticket_link}}" => 'url.com',
    "{{ticket_subject}}" => 'Hello World',
    "{{ticket_description}}" => 'foo bar',
];

$search = array_keys($replacements);
$replace = array_values($replacements);

$subject = "Dear {{user_name}}, we received your ticket #{{ticket_number}} with subject {{ticket_subject}}({{ticket_description}}). As a reminder, {{user_name}}, please close your ticket #{{ticket_number}}";
echo str_replace($search, $replace, $subject);

will output:

Dear Roman, we received your ticket #1234567890 with subject Hello World(foo bar). As a reminder, Roman, please close your ticket #1234567890

Sign up to request clarification or add additional context in comments.

9 Comments

brombeer, I had a dynamic replace values .. it can be one element or 10 element.. it depends of subject.
That's crucial information that should have been mentioned in your question in the first place.
If you "have a fixed list of search" how would you replace those 5 items with 10 elements? Please explain in your question what the different cases can be.
brombeer, yes I have a fixed list of search but in replace array element can be repeated several times. In subject I can have 2 ticket_number and 3 ticket_subject for example .. it is like email template with dynamic subject but fixed search.
Not sure I can follow. I doesn't make sense (to me) to have more replacement elements than elements to search for. Make sure they are at least the same size. Multiple {{ticket_subject}} in your $subject string will be replaced with the same element from $replace
|
0

According to the PHP documentation:

If replace has fewer values than search, then an empty string is used for the rest of replacement values

Your search array has more values than your replace array.

(https://www.php.net/manual/en/function.str-replace.php)

2 Comments

Bert Bijn, thank you very much. If I can to solve my example of code in another way?
It's not entirely clear to me what you're trying to achieve, but you should just put 5 items in your replace array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.