strtr

(PHP 4, PHP 5, PHP 7, PHP 8)

strtrTraduce certi caratteri

Descrizione

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

Questa funzione restituisce una copia di str, traducendo tutti i caratteri di from al corrispondente carattere indicato in to.

Se from e to hanno lunghezze differenti, i caratteri in più presenti nella stringa più lunga saranno ignorati.

Example #1 Esempio di uso di strtr()

<?php
$addr
= strtr($addr, "äåö", "aao");
?>

strtr() può essere eseguita con solo due argomenti. Se viene chiamata con due parametri si comporta in modo nuovo: from deve essere un matrice che contiene le coppie stringa -> stringa da sostituire nella stringa sorgente. strtr() cercherà sempre prima di incrociare il testo più lungo e *NON* tenterà di sostituire parti su cui ha già lavorato.

Example #2 Esempio di uso di strtr() con due parametri

<?php
$trans
= array("hello" => "hi", "hi" => "hello");
echo
strtr("hi all, I said hello", $trans);
?>

Questo visualizzerà:

hello all, I said hi

Nota: I parametri opzionali to e from sono stati aggiunti in PHP 4.0.0

Vedere anche ereg_replace().