How to replace string in php?

by tressie.damore , in category: PHP General , 8 months ago

How to replace string in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by addison , 8 months ago

@tressie.damore 

To replace a string in PHP, you can use the str_replace() function. Here's an example of how to use it:

1
2
3
4
5
6
<?php
$string = "Hello, World!";
$replaced_string = str_replace("World", "PHP", $string);

echo $replaced_string; // Output: Hello, PHP!
?>


In this example, the str_replace() function replaces the word "World" with "PHP" in the original string "Hello, World!". The function takes three arguments: the string you want to replace, the string you want to replace it with, and the original string.