PHP strtr() Function
Example
Replace the characters "ia" in a string with "eo":
Run Example »
Definition and Usage
The strtr() function translates specific characters in a string.
**Note:** If the from and to parameters have different lengths, they are truncated to the shortest length.
Syntax
strtr(
string,from,to
)
or
strtr(
string,array
)
| Parameters | Description |
|---|---|
| string | Required. Specifies the string to convert. |
| from | Required (unless an array is used). Specifies the characters to change. |
| to | Required (unless an array is used). Specifies the characters to change to. |
| array | Required (unless from and to are used). An array where the keys are the original characters and the values are the target characters. |
Technical Details
| Return value: | Returns the converted characters.
If both the array If a key contains an empty string (""), FALSE is returned. | | --- | --- | | PHP version: | 4+ |
More examples
Example 1
Replace the string "Hello world" with "Hi earth":
" Hi " , " world " => " earth " ) ; echo strtr ( " Hello world " , $arr ) ; ?>Run Example »
PHP String Reference Manual