Replacing Characters in a String in PHP
How To Replace a Group of Characters by Another Group?
While processing a string, you may want to replace a group of special characters with some other characters. For example, if you don't want to show user's email addresses in the original format to stop email spammer collecting real email addresses, you can replace the "@" and "." with something else. PHP offers the strtr() function with two format to help you:
Here is a PHP script on how to use strtr():
<?php $email = "firstname.lastname@example.org"; $map = array("@" => " at ", "." => " dot "); print("Original: $email\n"); print("Character replacement: ".strtr($email, "@.", "#_")."\n"); print("Substring replacement: ".strtr($email, $map)."\n"); ?>
This script will print:
Original: email@example.com Character replacement: joe#dev_fyicenter_moc Substring replacement: joe at dev dot fyicenter dot moc
To help you to remember the function name, strtr(), "tr" stands for "translation".
2016-10-13, 1038👍, 0💬
How to use the JSON to XML Conversion Tool at utilities-online.info? If you want to try the JSON to ...
Can You Add Values to an Array without Keys in PHP? Can You Add Values to an Array with a Key? The a...
What is EPUB 2.0 Metadata "dc:publisher" and "dc:rights" elements? EPUB 2.0 Metadata "dc:publisher" ...
Where to find tutorials on RSS specifications? I want to learn it to describe my API services. Here ...
How to use the JSON to XML Conversion Tool at freeformatter.com? If you want to try the JSON to XML ...