X words before and after keyword search in PHP-php Tutorial

Source: Internet
Author: User
PHP search for X words before and after Keywords
// The word cannot be split. // multiple identical words appear in the content. only the first one can be searched. // The last part is omitted "... "Representation // fuzzy query is allowed. // The keyword can be multiple words.


Please help me check whether there are any problems with the code (if it is missing, I did not think about it. how can I optimize the code and other problems)

function getSPContent($content, $keyword){    $keyword = trim($keyword);    $content = trim(strip_tags($content));    $keyword2 = explode(" ", $keyword);    $content2 = explode(" ", $content);    $start_pos = "x";    if(count($keyword2) > 1){        foreach ($content2 as $k => $v) {            preg_match("/{$keyword2[0]}/", $v, $matches);            if (!empty($matches)) {                foreach($keyword2 as $x=>$y){                    preg_match("/{$keyword2[$x]}/", $content2[$k+$x], $matches);                    if (!empty($matches) && $x == count($keyword2)-1) {                        $start_pos = $k;                        break;                    }elseif(!empty($matches)){                        continue;                    }else{                       break;                    }                }            }            if($start_pos != "x"){                break;            }        }    }else{        foreach ($content2 as $k => $v) {            preg_match("/{$keyword}/", $v, $matches);            if (!empty($matches)) {                $start_pos = $k;                break;            }        }    }    $max = 50;    $len = count($content2) - 1;    $content3 = "";    if ($len < $max) {        return $content;    } else {        if ($start_pos == "x") {            for ($i = 0; $i < $max; $i++) {                $content3 = $content3 . " " . $content2[$i];            }            return ltrim($content3) . "...";        } elseif ($start_pos <= $max / 2) {            for ($i = 0; $i < $start_pos + $max / 2; $i++) {                $content3 = $content3 . " " . $content2[$i];            }            return ltrim($content3) . "...";        } elseif ($start_pos + $max / 2 >= $len) {            for ($i = ($start_pos - $max / 2); $i <= $len; $i++) {                $content3 = $content3 . " " . $content2[$i];            }            return "..." . ltrim($content3);        } else {            for ($i = $start_pos - $max / 2; $i < $start_pos + $max / 2; $i++) {                $content3 = $content3 . " " . $content2[$i];            }            return "..." . ltrim($content3) . "...";        }    }}


Reply to discussion (solution)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.