Tools, FAQ, Tutorials:
Replacing Characters in a String in PHP
How To Replace a Group of Characters by Another Group?
✍: FYIcenter.com
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 = "joe@dev.fyicenter.com";
$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: joe@dev.fyicenter.moc Character replacement: joe#dev_fyicenter_com Substring replacement: joe at dev dot fyicenter dot com
To help you to remember the function name, strtr(), "tr" stands for "translation".
⇒ Understanding PHP Arrays and Their Basic Operations
⇐ Applying UUEncode to a String in PHP
2016-10-13, ∼3128🔥, 0💬
Popular Posts:
How To Read Data from Keyboard (Standard Input) in PHP? If you want to read data from the standard i...
How to use the JSON to XML Conversion Tool at utilities-online.info? If you want to try the JSON to ...
How to install .NET Framework in Visual Studio Community 2017? I have the Visual Studio Installer in...
FYIcenter.com Online Tools: FYIcenter JSON Validator and Formatter FYIcenter JSON to XML Converter F...
What is Azure API Management Developer Portal? Azure API Management Developer Portal is an Azure Web...