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, ∼2358🔥, 0💬
Popular Posts:
How to use the urllib.request.Request object to build more complex HTTP request? The urllib.request....
How to create a navigation file like navigation.xhtml for an EPUB 3.0 book? At least one navigation ...
Where to find tutorials on Python programming language? I want to learn Python. Here is a large coll...
How to use the "rewrite-uri" Policy Statement for an Azure API service operation? The "rewrite-uri" ...
How to create Hello-3.1.epub with WinRAR? I have all required files to create Hello-3.1.epub. To cre...