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.moc"; $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_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, 875👍, 0💬
Popular Posts:
How to start Visual Studio Command Prompt? I have Visual Studio 2017 Community version with Visual C...
Where to find tutorials on HTML language? I want to know how to learn HTML. Here is a large collecti...
How to use the API operation 2017 version setting "Rewrite URL template"? The API operation setting ...
Where Is the Submitted Form Data Stored in PHP? When a user submit a form on your Web server, user e...
How To Upload Files into Database in PHP? To store uploaded files to MySQL database, you can use the...