replace a character in a string in php

To replace a character(s) in a string in PHP, you can use the str_replace() function.

The syntax for the str_replace() function is as follows:

main.php
str_replace($search, $replace, $subject);
42 chars
2 lines

Here, $search is the character(s) that you want to replace, $replace is the new character(s) with which you want to replace the old character(s), and $subject is the string that you want to modify.

Here's an example:

main.php
$string = 'Hello, World!';
$new_string = str_replace(',', '|', $string);
echo $new_string;
91 chars
4 lines

In this example, we're replacing the comma , in the original string with a pipe |. The output of this code will be:

main.php
Hello| World!
14 chars
2 lines

As you can see, the comma has been replaced with a pipe in the new string.

gistlibby LogSnag