dimanche 28 juillet 2019

a program for the if else statement with nested loop and regex

  1. there are 2 function in the program. public and private function.

  2. $merge_list is the db, with $article[' '] the place in the db to be searched.

  3. there is a text file with a given list of words which needed to be excluded from the db.

  4. the private function is called in the public function for the processing.

  5. i have successfully written a program to do unset the given words from the text file,

  6. but when the words from the text file are 'au' or 'awa' a certain if else must be applied with this regex, '/\bau\b/si' .

  7. if any one could complete the program i would really be very thankful to you.

completion needed in the //if and else if part only.

if($ng_word == 'au') {
            //if the word is au
            preg_match_all('/\bau\b/si', $article['article_headline1'], $ng) ||
            preg_match_all('/\bau\b/si', $article['article_headline2'], $ng) ;

if (array_search('au', $ng) !==FALSE) {
               var_dump($ng);
            }

<?php
public function dc_check_ng_word(&$merge_list){ //$merge_list is the db
        $ng_file = explode("\n", file_get_contents(self::NG_FILE)); //the text file
        $ng_list = [];
        foreach($merge_list as $key => $article) {
            //nested loop
            foreach($ng_file as $ng_word) {
                if(!$this->ng_word_match($article, $ng_word)) { //see the private function below
                    $ng['keyword'] = $ng_word;
                    $ng['url'] = $article['url'];
                    $ng_list[] = $ng;
                    unset($merge_list[$key]); //erasing the $ngword from the $merge_list
                }

            }
        }
    }
    //the is a list of words in a text file but if the condition of 'au' and 'awa'  a different conditon need to be writtien.
    //ng word check function
    private function ng_word_match($article, $ng_word) {
        if($ng_word == 'au') {
            //if the word is 'au' 
            //i have tried with  preg_match_all('/\bau\b/si', $article['article_headline1'], $ng)

        }else if($ng_word == 'awa') {
            //if the word is 'awa'

        }else {
            // if other words
            if (preg_match('/'.$ng_word.'/', $article['article_headline1']) || //searching the ng word in the db
                preg_match('/'.$ng_word.'/', $article['article_headline2']) ||
                preg_match('/'.$ng_word.'/', $article['article_headline3']) ||
                preg_match('/'.$ng_word.'/', $article['article_headline4']) ||
                preg_match('/'.$ng_word.'/', $article['article_headline5']) ||
                preg_match('/'.$ng_word.'/', $article['article_sentence1']) ||
                preg_match('/'.$ng_word.'/', $article['article_sentence2']) ||
                preg_match('/'.$ng_word.'/', $article['article_sentence3']) ||
                preg_match('/'.$ng_word.'/', $article['article_sentence4']) ||
                preg_match('/'.$ng_word.'/', $article['article_sentence5']))
            {
                return false;
            }
        }
        return true;
    }
    ?>

//the if and else section is only the problem i couldn't solve

Aucun commentaire:

Enregistrer un commentaire