PHP strtr() Function
In this chapter you will learn:
- Definition for PHP strtr() Function
- Syntax for PHP strtr() Function
- Parameter for PHP strtr() Function
- Return for PHP strtr() Function
- Note for PHP strtr() Function
- Example - Replace the characters "jav" in the string with "PHP"
- Example - Replace the string "Hello world" with "Hi java2s.com"
Definition
The strtr() function translates certain characters in a string.
Syntax
PHP strtr() Function has the following syntax.
strtr(string,from,to)
or
strtr(string,array)
Parameter
Parameter | Is Required | Description |
---|---|---|
string | Required. | String to translate |
from | Required (unless array is used). | What characters to change |
to | Required (unless array is used). | What characters to change into |
array | Required (unless to and from is used). | An array containing what to change from as key, and what to change to as value |
Return
PHP strtr() Function returns the translated string.
Note
If the from and to parameters are different in length, both will be trimmed to the length of the shortest.
Example
Replace the characters "jav" in the string with "PHP":
<?php
echo strtr("java2s . com ","jav","PHP");
?>
The code above generates the following result.
Example 2
Replace the string "Hello world" with "Hi java2s.com":
<?php
$arr = array("Hello" => "Hi", "world" => "java2s.com");
echo strtr("Hello world",$arr);
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP substr() Function
- Syntax for PHP substr() Function
- Parameter for PHP substr() Function
- Return for PHP substr() Function
- Example - Use substr to get sub string
- Example - Extract from the end of a string
Home » PHP Tutorial » PHP String Functions