Support PHP Version: PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0, PHP 8.1, PHP 8.2, PHP 8.3 With Latest All Version Support.
Hello Friends Today, through this tutorial, I will tell you How do you use `str_replace()` function using PHP, PHP 8, PHP 8.1, PHP 8.2, PHP 8.3 With Example. The `str_replace()` function in PHP 8.1 and PHP 8.2 is used to replace occurrences of a substring within a string with another substring. Here’s how you can use it with an example:
<?php // Original string $string = "The quick brown fox jumps over the lazy dog."; // Substring to be replaced $substring_to_replace = "lazy"; // Replacement string $replacement_string = "energetic"; // Replace occurrences of the substring $new_string = str_replace($substring_to_replace, $replacement_string, $string); // Output the modified string echo $new_string; ?>
In this example:
1. `$string` is the original string.
2. `$substring_to_replace` is the substring to be replaced.
3. `$replacement_string` is the string that will replace occurrences of `$substring_to_replace` in `$string`.
4. `str_replace()` function performs the replacement.
The output will be:
The quick brown fox jumps over the energetic dog.
This function is useful for tasks like search-and-replace operations within strings, modifying text, or sanitizing input. It’s commonly used in PHP for string manipulation tasks.