Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP substr_replace() Function
Next article icon

PHP | strtr() for replacing substrings

Last Updated : 26 May, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
It replaces given substring in a string with another given string. We can also use it to do multiple replacements by passing an array of pairs. Examples:
  Input : $str = "Hmrrb GmmksfbrGmmks";          $from = "rbm";          $to = "loe";  Output : Hello GeeksforGeeks    Input : $str = "Hello world";          $arr = array("Hello" => "I Love", "world" => "GeeksforGeeks");  Output : I Love GeeksforGeeks  
Syntax :
  string strtr ( string $string, string $from, string $to)    OR    string strtr (string $string, array $from_to_pairs)  

Parameters :This function accepts three/two parameters and all of them are mandatory to be passed.    Syntax 1 :       1. $string: This parameter represents the given input string.       2. $from: This parameter represents the sub string that is to be translated.       3. $to: This parameter represents the translated sub string of "from" sub string.    Syntax 2 :       1. $string: This parameter represents the given input string.       2. $translating_pairs: This parameter represents the array containing respective From-to pairs.

Return Value :This function returns a string in which all the characters of from sub string are replaced by to sub string in the given string.

Note that, if from and to have different lengths, then output will be co-related with that of the shortest. Below programs illustrates the strtr() function in PHP:

Program 1 : PHP
<?php   // original string $str = "GzzksworGzzks is zverything.";   // from and to terms $from = "zw"; $to = "ef";  // calling strtr() function $resStr = strtr($str, $from, $to);   print_r($resStr);   ?> 

Output :

  GeeksforGeeks is everything.  
Program 2 : PHP
<?php   // original string $str = "Hi there";   // array declaring from-to pairs $arr = array("Hi" => "Be", "there" => "Happy");  // calling strtr() function $resStr = strtr($str, $arr);   print_r($resStr);   ?> 

Output :

  Be Happy  
Reference : http://php.net/manual/en/function.strtr.php

Next Article
PHP substr_replace() Function

P

Prasad_Kshirsagar
Improve
Article Tags :
  • Misc
  • Web Technologies
  • PHP
  • PHP-string
Practice Tags :
  • Misc

Similar Reads

  • PHP strtok() for tokening string
    Like C strtok(), PHP strtok() is used to tokenize a string into smaller parts on the basis of given delimiters It takes input String as a argument along with delimiters (as second argument). Syntax : string strtok ( string $string, string $delimiters ) Parameters :This function accepts two parameter
    2 min read
  • PHP substr_replace() Function
    The substr_replace() function is a built-in function in PHP and is used to replace a part of a string with another string. The index in the original string from which the replacement is to be performed needs to be passed as a parameter. If desired, the length up to which the replacement is to be don
    3 min read
  • How to replace String in PHP ?
    Replacing a string in PHP involves substituting parts of a string with another string. Common methods include str_replace() for simple replacements, preg_replace() for pattern-based replacements, substr_replace() for positional replacements, and str_ireplace() for case-insensitive replacements. Each
    3 min read
  • PHP str_replace() Function
    In this article, we will see how to replace the occurrence of the search string with the replacing string using the str_replace() function in PHP, along with understanding their implementation through the examples. The str_replace() is a built-in function in PHP and is used to replace all the occurr
    3 min read
  • PHP to check substring in a string
    In this article, we will see how to check the substring in a string. We are given two strings & we have to check whether the second string is a substring of the first string or not using PHP built-in strpos() function. This function is case-sensitive, which means that it treats upper-case and lo
    2 min read
  • PHP str_pad to print string patterns
    str_pad: Pad a string to a certain length with another string. Syntax:- str_pad (input, pad_length, pad_string_value, pad_type) It returns the padded string. Parameters Description input:-The input string. pad_length:-If the value of pad_length is negative, less than, or equal to the length of the i
    1 min read
  • PHP Reverse a String
    Reversing a string in PHP refers to rearranging a given string's characters in reverse order, starting from the last character to the first. This task is often used in text manipulation or algorithm challenges, highlighting PHP's string-handling capabilities. Examples: Input : GeeksforGeeksOutput :
    3 min read
  • How to replace a text in a string with another text using PHP ?
    A string is a sequence of one or more characters. A string is composed of characters, each of which can be replaced easily in the script. A large number of in-built PHP methods can be used to perform string replacements. Table of Content Using str_replace() method - The str_replace() Using str_irepl
    4 min read
  • PHP str_starts_with() Function
    This is a predefined function in PHP 8 that is used to perform case-sensitive searches on a given string. str_starts_with() generally checks for the string if it is beginning with the substring or not. If the string begins with the substring then str_starts_with() will return TRUE otherwise it will
    2 min read
  • JavaScript string replace() Method
    JavaScript replace() method is used for manipulating strings. It allows you to search for a specific part of a string, called a substring, and then replace it with another substring. What's great is that this method doesn't alter the original string, making it ideal for tasks where you want to maint
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences