php根据关键词获取文章一句话

获取关键词在字符串中出现的次数的位置
/**
         * @param $str
         * @param $char
         * @return array
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @name: getCharpos2
         * @describe: Get the position of all occurrences of the character in the string according to the keyword
         */
        public function getCharpos2($str, $char)
        {
            $j     = 0;
            $arr   = [];
            $count = substr_count($str, $char);
            for($i = 0; $i < $count; $i++){
                $j     = strpos($str, $char, $j);
                $arr[] = $j;
                $j     = $j + 1;
            }
            return $arr;
        }
根据次数获取语句
  /**
         * @param $str
         * @param $key
         * @return bool|string
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @name: duan
         * @describe:
         */
        public function get_ju($str, $key)
        {
            $num     = strpos($str, $key);
            $arr     = $this->getCharpos2($str, ".");
            $min_arr = [];
            $max_arr = [];
            foreach($arr as $k => $v){
                if($v < $num) {
                    $min_arr[] = $v;
                }
                else {
                    $max_arr[] = $v;
                }
            }
            if(empty($min_arr)) {
                $begin = 0;
            }
            else {
                $begin = max($min_arr) + 1; //获取起点值
                $jian  = max($min_arr);
            }
            if(empty($max_arr)) {
                $end = 100*100;
            }
            else {
                $end = min($max_arr) - $jian;
            }
            $res_str = substr($str, $begin, $end);
            return $res_str;
        }
测试
 $str = "An equilibrium statistical theory of coherent structures is applied to midlatitude 
            bands in the northern and southern hemispheres of Jupiter. The theory imposes energy and 
            circulation constraints on the large-scale motion and uses a prior distribution on potential 
            vorticity fluctuations to parameterize the small-scale turbulent eddies. Nonlinearly stable 
            coherent structures are computed by solving the constrained maximum entropy principle governing 
            the equilibrium states of the statistical theory. The theoretical predictions are consistent with the 
            observed large-scale features of the weather layer if and only if the prior distribution has anticyclonic 
            skewness, meaning that intense anticyclones predominate at small scales. Then the computations show that 
            anticyclonic vortices emerge at the latitudes of the Great Red Spot and the White Ovals in the southern band, whereas 
            in the northern band no vortices form within the zonal jets. Recent observational data from the Galileo mission support the 
            occurrence of intense small-scale anticyclonic forcing. The results suggest the possibility of using equilibrium statistical theory for 
            inverse modeling of the small-scale characteristics of the Jovian atmosphere from observed features.";
$key="theoretical";
var_dump($this->get_ju($str,$key));
//输出结果
string(257) " The theoretical predictions are consistent with the 
            observed large-scale features of the weather layer if and only if the prior distribution has anticyclonic 
            skewness, meaning that intense anticyclones predominate at small scales."
测试地点

sci机器人写作系统

评论

(= ̄ω ̄=)··· 暂无内容!

回复

您还未登录,请先登录或者注册

微信扫一扫
关注该公众号