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机器人写作系统

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值