PHP

Topic: String

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:    strtr(string, from, to) - Replacing each character in "from" with the corresponding character in "to".    strtr(string, map) - Replacing each substring in "map" with the corresponding substring in "map".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.mocCharacter replacement: joe#dev_fyicenter_mocSubstring replacement: joe at dev dot fyicenter dot mocTo help you to remember the function name, strtr(), "tr" stands for "translation".

Browse random answers:

How To Count the Number of Characters in a String?
How Many ways to Remove Leading and Trailing White Spaces?
How To Remove Leading and Trailing Spaces from User Input Values?
How to Find a Substring from a Given String?
What Is the Best Way to Test the strpos() Return Value?
How To Take a Substring from a Given String?
How To Replace a Substring in a Given String?
How To Reformat a Paragraph of Text?
How To Convert Strings to Upper or Lower Cases?
How To Convert Leading Characters to Upper Case?
How To Compare Two Strings with strcmp()?
How To Convert Strings to Hex Numbers?
How To Generate a Character from an ASCII Value?
How To Convert a Character to an ASCII Value?
How To Split a String into Pieces?
How To Join Multiple Strings into a Single String?
How To Replace a Group of Characters by Another Group?
How Many Escape Sequences Are Recognized in Single-Quoted Strings?
Can You Specify the "new line" Character in Single-Quoted Strings?
What Are the Special Characters You Need to Escape in Double-Quoted Stings?
How To Include Variables in Double-Quoted Strings?
How Many Ways to Include Array Elements in Double-Quoted Strings?
How To Access a Specific Character in a String?
How To Assigning a New Character in a String?
How To Concatenate Two Strings Together?
How To Compare Two Strings with Comparison Operators?
How To Convert Strings to Numbers?
How To Count the Number of Characters in a String?
How To Remove the New Line Character from the End of a Text Line?
How To Remove Leading and Trailing Spaces from User Input Values?
How to Find a Substring from a Given String?
How To Remove the New Line Character from the End of a Text Line?
How to Find a Substring from a Given String?
How To Take a Substring from a Given String?
How To Replace a Substring in a Given String?
How To Reformat a Paragraph of Text?