Jump to: navigation, search

Php str replace

From w3cyberlearnings

Contents

PHP function str_replace

This function replaces all occurrences of the search string with the replacement string. It is case sensitive. This function is similar to php str_ireplace

Syntax str_replace

  • search: string to search for replace
  • replace: string to replace
  • count
str_replace (string, replace, count);

Example 1

<?php

$str = "You are a great leader";
$new_str = str_replace("great", "good", $str);
echo $new_str;
?>

Output

You are a good leader

Example 2

<?php
$str = "All you can eat, all you can say, all you can think.";
$new_str = str_replace("all", "nothing", $str, $count);

echo $new_str;
echo '<br/>';
echo 'Total Replace: ' . $count;
?>

Output

All you can eat, nothing you can say, nothing you can think.
Total Replace: 2

Related Functions


Navigation
Web
SQL
MISC
References