- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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);
Advertisements