I can't remove strlen from this function to count. How can I use array and strlen in one function like this?
PHP
if(isset($_POST['cek'])) {
function match( $text, $pattern ) {
$count=0;
$split_words = explode(' ', $text );
$cek = 0;
$n=strlen($split_words);
$m=strlen($pattern);
for ($i = 0; $i <= $n - $m; $i++) {
$j = 0;
while ($j < $m && $split_words[$i + $j] == $pattern[$j]){
echo $j++;
}
if ($j >= $m) {
echo $cek++;
}
}
if ($cek > 0) {
echo "matched";
$count;
} else {
echo "didn't match ";
}
}
$banned_words = array('badword1','badword2','badword3','badword4','badword5','badword6','badword7');
$teks = $_POST['teks'];
echo match($teks, $banned_words);
$count2 = match($teks,$banned_words);
if ($count2 != 0) {
echo $count2;
echo 'blocked!';
}else{
echo $count2;
echo 'Text valid';
}
}
HTML
<form method="post">
<input type="text" name="teks"/>
<button type="submit" name="cek">Submit</button>
OUTPUT
Warning: strlen() expects parameter 1 to be string, array given in C:\xampp\htdocs\kmp\bs.php on line 7 Warning: strlen() expects parameter 1 to be string, array given in C:\xampp\htdocs\kmp\bs.php on line 8 0matched Warning: strlen() expects parameter 1 to be string, array given in C:\xampp\htdocs\kmp\bs.php on line 8 0matchedText valid
$m=strlen($pattern);this line, then you pass$patternas an array - which you can't count the length of the string - because it's not a string, its an array. If you can explain a bit more specific what you want the function to do, there is likely an easier approach to it. - Qirel$split_words? - chris85count()instead ofstrlen()- mferly