w3resource

PHP: strtr() function

Description

The strtr() function is used to translate certain characters in a string.

Version:

(PHP 4 and above)

Syntax:

strtr(string_name, from_string, to_string) 

Parameters:

Name Description Required /
Optional
Type
string_name The string to be translated. Required String
from_string Contains characters to be translated. Required String
to_string Contains characters to be translated with. Required String

Another Syntax:

strtr(string_namer, translating_array)

Parameters:

Name Description Required /
Optional
Type
string_name The string to be translated Required String
translating_array Contains key value pairs. If values of the array elements are matched in the input string, then those characters in the input string are replaced with the corresponding keys of those values. Required Array

Return value:

Returns the translated string.

Value Type: String.

Pictorial Presentation

string_strtr

Example:

<?php
$string = 'Good Marning....';
echo  strtr($string, 'Ma', 'mo');
?>

Output:

Good morning....

View the example in the browser

See also

PHP Function Reference

Previous: strtoupper
Next: substr_compare



PHP: Tips of the Day

Get the full URL in PHP

Have a look at $_SERVER['REQUEST_URI'], i.e.

Example:

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

(Note that the double quoted string syntax is perfectly correct)

If you want to support both HTTP and HTTPS, you can use

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Editor's note: using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.

Ref : https://bit.ly/2ZYItNJ