PHP nested array search

前端 未结 5 696
一整个雨季
一整个雨季 2021-01-15 17:37

I\'m new in PHP

I have an array like this

$suspiciousList = array(
array (\"word\" => \"badword1\", \"score\" => 400, \"type\" => 1), 
array         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-15 17:47

    As Jirka Helmich suggested you could remove whitespaces (and maybe other special chars) and then search the string to find words from your array.

    public function searchForBadWords($strippedText) {
         foreach($suspiciousList as $suspiciousPart) {
              $count = substr_count($strippedText, $suspiciousPart['word']);
              //you can use str_replace here or something, it depends what you want to achive
         }
    }
    

    Problem is if you have words like blablabad wordblabla and you remove spaces to normal words could become bad words blablabadwordblabla (know what I mean?) :D

    Cheers

    Edit: So Ahmad I see you just get words recognizing them by " " on the beginning/end(in shortcut). Maybe you should try to implement both methods, yours with single words and this above with substring searching. It depends also how much you care about performance. Maybe you should try do some reserches or sth to see how effective it is?:D

提交回复
热议问题