mahisharma2
New member
Hi Members,
Im a newbie to the forum and learning PHP i have an assignment where if there is a badword it replaces it with **** it loops the array
what happens it repeats the sentence the same amount of time as the number of words i the array, trying to figure out howto to diplay
the sentence only once, below is the HTML form and the PHP code, thanks for your assistance:
Im a newbie to the forum and learning PHP i have an assignment where if there is a badword it replaces it with **** it loops the array
what happens it repeats the sentence the same amount of time as the number of words i the array, trying to figure out howto to diplay
the sentence only once, below is the HTML form and the PHP code, thanks for your assistance:
Code:
01
<!DOCTYPE html>
02
<html>
03
<head>
04
<title>ISBN checker</title>
05
</head>
06
<body>
07
08
<form action="badwords.php" method="post">
09
<label> Feedback/ Comments are welcome</label>
10
<br>
11
12
<input type="text" name="words">
13
<input type="submit" name="submit" value="send">
14
</form>
15
16
</body>
17
</html>
Code:
<?php
02
03
/*1. create a global variable that contains swear words in an array.
04
2. create variables that contain $_POST values for the
05
textarea and submit button.
06
3. go through the area with the foreach loop.
07
4. if statement $badwords str_replace with ****
08
*/
09
10
$badwords = ['badword', 'badword', 'badword', 'badword', 'badword', 'badword', 'badword', 'badword'];
11
12
$words = $_POST['words'];
13
14
foreach($badwords as $badword){
15
16
if($badwords ==true){
17
$words = str_replace($badword, '****', $words);
18
echo $words;
19
}
20
}
21
22
23
?>