php

How to replace a word in a string in PHP

In this tutorial, we are going to see how to replace a word in a string in PHP. You can use str_replace() function in PHP to replace all occurrences of a word in a string. In the following example, the word “google” is replaced with the word “StackHowTo”.
 

How to replace a word in a string in PHP

The str_replace() function is case sensitive. If you want to perform a case-insensitive match, you can use the str_ireplace() function.

<?php
	$str = 'Welcome to google.com';
	echo str_replace("google", "waytolearnx", $str);
?>

Output:

Welcome to StackHowTo.com
[st_adsense] mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *