PHP - strtr
Trieda
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ázov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| $str | string | The string where the values are replaced. | |
| $from | string | The substring to be replaced. | |
| $to | string | The substring that will replace the original substring. | |
| $replace_pairs | array | 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
