2
votes

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

2
$m=strlen($pattern); this line, then you pass $pattern as 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
Are you trying to count the number of elements in $split_words? - chris85
If you are looking to count the number of items in the array, you can use count() instead of strlen() - mferly
@Qirel I wanna use brute force algorithm to match data. For example I will input data like this 'badword1 make me badword2'. I want to compare the data from input data with array data(banned_words). So I will convert input data to array data then compare them in order to count 'the badwords'. - Tatif Isu
How about a regex approach? eval.in/586251 You probably also should add word boundaries. If that works let me know and I'll post as answer. - chris85

2 Answers

0
votes

Hope this will help. You can count the string length like this:

<?php

$text = 'Hello this is a string';

$split_words = explode(' ', $text );

$count = 0;

foreach($split_words as $value){
    $count+= strlen($value);
};

echo $count;
0
votes

Q: How to fix Warning: strlen() expects parameter 1 to be string, array given?

Short answer: To fix this error, you just need to pass string as an argument instead of array. Like it says.

It seems, that one of your strlens' parameters is not a string (either $split_words or $pattern)

Moreover $split_words seems to be an array because it's the result of explode(). If you want to count number of elements in array use count instead of strlen()

Example of strlen()

$string = 'test';
echo strlen($string);

prints: 4