
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP program to replace a word with a different symbol in a sentence
To replace a word with a different symbol in a sentence, the PHP code is as follows −
Example
<?php $my_str = "This is a sample only"; $search_str = array("sample", "This"); $replace_str = array("simple"); $result = str_replace($search_str, $replace_str, $my_str); print_r("The final replaced string is :"); print_r($result); ?>
Output
The final replaced string is : is a simple only
Here, a string is defined, and the string that needs to be replaced with a different string is placed inside an array and arranged to a variable −
$my_str = "This is a sample only"; $search_str = array("sample", "This"); $replace_str = array("simple");
The ‘str_replace’ function is used to replace the value with a different specified value. The result is printed on the screen −
$result = str_replace($search_str, $replace_str, $my_str); print_r("The final replaced string is :"); print_r($result);
- Related Articles
- Python Program to replace a word with asterisks in a sentence
- Java Program to replace a word with asterisks in a sentence
- PHP program to find the first word of a sentence
- C# Program to replace a character with asterisks in a sentence
- Java program to reverse each word in a sentence
- Python program to reverse each word in a sentence?
- C program to Replace a word in a text by another given word
- Print longest palindrome word in a sentence in C Program
- Python program to remove all duplicates word from a given sentence.
- How to replace all occurrences of a word in a string with another word in java?
- Java program to count the characters in each word in a given sentence
- C++ program for length of the longest word in a sentence
- Program to replace all the characters in of a file with '#' except a particular word in Java
- Capitalize a word and replace spaces with underscore - JavaScript?
- Write a C program to calculate the average word length of a sentence using while loop

Advertisements