NOVINKA: Najžiadanejšie rekvalifikačné kurzy teraz s 50% zľavou + kurz AI ZADARMO. Nečakaj, táto ponuka dlho nevydrží! Zisti viac:

PHP - strtr

Trieda

Koreň \ Bez triedy

Metóda - strtr

(PHP 4, PHP 5, PHP 7)

The strtr() function is similar to the str_replace() function. However, there is a small difference. This function does not replace strings that already have been replaced (check examples below).

The function can be used in two different ways. You can just set a dictionary, or you have to insert two strings.

Procedurálne

  • function strtr (string $str, string $from, string $to, array $replace_pairs) : string
  • function strtr (string $str, string $from, string $to, array $replace_pairs) : string

Parametre

NázovDátový typPredvolená hodnotaPopis
$strstring

The string where the values are replaced.

$fromstring

The substring to be replaced.

$tostring

The substring that will replace the original substring.

$replace_pairsarray

The dictionary containing original substrings and their replacements.

Mávratovej hodnoty

Vracia: string

The function returns the substring after replacing.

Príklady

<?php
        $dictionary = [
                ':)' => '<img src="smile.png" alt="smile" />',
                ':D' => '<img src="grin.png" alt="grin" />',
        ];
        echo strtr('Hi :) I feel fine because I have found ICT :D', $dictionary);

Let's also check the difference between those mentioned functions:

<?php
    echo str_replace(['a', 'b', 'c'], ['b', 'c', 'a'], 'abc') . "<br />";
    echo strtr('abc', [
        'a' => 'b',
        'b' => 'c',
        'c' => 'a'
    ]);

Súvisiace manuály

      • function str_replace (mixed $search, mixed $replace, mixed $subject, int &$count) : mixed
      Aktivity