1

I have written this code to take the sting from the textbox and want to replace any badwords with "***" but I don't know why I am getting one extra "1" at the end..you can have a look of output in the picture.

here is my code

<!DOCTYPE>
<html>
  <head>
    <title> Mind Your Language </title>
  </head>
  <body>
    <form action="" method="post">
      <input type="text" name="string">
      <input type="submit" name="submit" value="Filter">
    </form>
  </body>
</html>

<?php

if(isset($_POST['submit'])) {
  
  $badWords = ['badword1', 'badword2', 'badword3', 'badword4', 'badword5'];
  $string = $_POST['string'];
  
  foreach($badWords as $badWord) {
    $string1 = str_replace($badWord, "***", $string);
  }
  echo print_r($string1);
}
?>

enter image description here

w3spi
  • 4,380
  • 9
  • 47
  • 80
Shashank
  • 294
  • 3
  • 13

1 Answers1

1

print_r() displays information about a variable in a way that's readable by humans.

If given a string, integer or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements. Similar notation is used for objects.

When the return parameter is TRUE, this function will return a string. Otherwise, the return value is TRUE.

Either you need to use echo print_r($string1, true); or simply print_r($string1);

Omi
  • 3,954
  • 5
  • 21
  • 41