Replace the words from the text with the keys of the mass



  • There's a pattern.

    Array
    (
        [0] => Soflyy T-Shirt (blue)
        [1] => 999-B
        [2] => 999-X
        [3] => 15 
        [4] => blue
        [5] => red
    )
    

    And there's a text of the species.

    "Пример текста [0], простой [1] пример текста [2]"
    

    It should be replaced in the text [0], [1] and (e) by the relevant keys in the body.



  • We'll take the keys to the array_keys, we'll add the brackets, and then we'll change through str_relace.

    $ar = Array
    (
        'Soflyy T-Shirt (blue)',
        '999-B',
        '999-X');
        $str = "Пример текста [0], простой [1] пример текста [2]";
    

    echo str_replace ( array_map(function ($v) { return '['.$v.']'; }, array_keys($ar)), $ar, $str);

    result

    Пример текста Soflyy T-Shirt (blue), простой 999-B пример текста 999-X




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2