Home Backend Development PHP Tutorial 帮小弟我把这函数精简下啊

帮小弟我把这函数精简下啊

Jun 13, 2016 pm 01:26 PM
ar keyword quot split varchar

帮我把这函数精简下啊,,

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php $varchar='德国和丹麦都在第一场比赛1-0获胜,在次轮比赛中,德国2-1击败荷兰,两场小组赛战罢,德国队虽然拿到了6分,但依然未能保证出线,最后一场如果输给丹麦仍然有出局可能。丹麦上一场在0-2落后的情况下连追两球,但最后还是2-3输给葡萄牙,将小组次席拱手让给葡萄牙。

丹麦想要出线必须力争击败德国,好在他们也不缺乏击败德国的感觉。在两队最近3场交手中,丹麦令人吃惊地2胜1平占据上风,最近一次交手是在2010年8月,丹麦在落后两球情况下扳平比分。两队上一次在欧洲杯交手则是1992年欧洲杯决赛,当时正是丹麦童话的巅峰,北欧人2-0击败德国夺冠。

在德国队前两场比赛中,戈麦斯成为最大的亮点。超级马里奥连续两场比赛包揽了德国3粒进球,恐怖的是他进3球居然只花了6脚射门。戈麦斯目前以3球排名射手榜首位,自从1976年欧洲杯穆勒单届射进4球以来,德国队此后36年还没有任何球员欧洲杯单届进球超过3个。左边锋波多尔斯基在此前两场小组赛均打满全场,国家队出场次数达到99场,本场有望实现百场。

双方都没有对首发阵容进行大的调整,德国队只是因为博阿滕停赛而派上了本德,丹麦也只换上鲍尔森,队内元老罗梅达尔因伤缺席。本场德国前锋波多尔斯基迎来国家队第100场比赛,小将本德则是首次在国家队首发。

第2分钟,赫迪拉禁区前沿左侧传中,中路无人拿到皮球,后点穆勒胸部停球后左脚抽射稍稍高出。第5分钟,丹麦左侧角球,本特纳远点头球攻门被诺伊尔拿到。1分钟后,德国迅速反击,厄齐尔左侧直塞,波多尔斯基传中,克亚尔解围不力,穆勒门前3米处射门被安德森神奇化解。第12分钟,戈麦斯禁区外围左脚远射高出。';


$keyword="本德|鲍尔森";
//$keyword="射门|本德|鲍尔森";
//$keyword="丹麦|角球|头球|神奇";


function pipeidu($keyword,$varchar,$length){
    $keyexp=explode('|',$keyword);
    $keyNum=count($keyexp);
$ar = preg_split('/('.$keyword.')/', $varchar, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE);
for($j=0; $j<count($ar)-1; $j++) {
  $t = $ar[$j][1];
  $r[$t] = 0;
  for($i=$j; $i<count($ar); $i++) {
    if($ar[$i][0] == '本德'||$ar[$i][0] == '鲍尔森') {  //[color=#FF0000]求此处适应一个或多个词组,越精简越好[/color]。
      if($ar[$i][1]+strlen($ar[$i][0]) - $t < $length) $r[$t]++;
      else $r[$t=$ar[$i][1]] = 0;
    }
  }
}
if(isset($r)&&count($r)>0){
arsort($r);
$r=array_keys($r);
return $r=substr($varchar,$r[0],$length);
}
else{return $r=substr($varchar,0,$length);}
}
?>
Copy after login


------解决方案--------------------
呵呵, 原来你就喜欢用这些半残废封装.
------解决方案--------------------
to qq120848369
lz 的代码不是如你所说 http://topic.csdn.net/u/20120619/02/c024bcf0-9c61-4e61-b58c-2844f0af2310.html
1,扫一遍找出所有的关键字。
2,再扫一遍,计算出每个end_index位置之前的关键字总数,同时计算180个字符之前的beg_index位置的关键字个数差值。
3,对关键字差值最大的180长度的段,strrstr,strstr分别头尾找到一个关键字,把多余的部分切掉,这样可以让字符再少一点。

的思路做的吗?
1、用 preg_split 完成找出所有的关键字
2、从每一个关键字出现的位置开始统计指定长度内关键字出现的次数(双重循环)

当然 lz 的代码也是用可优化的地方的,比如
外层循环中的
$t = $ar[$j][1];
 $r[$t] = 0;
与内层循环中的
 $r[$t=$ar[$i][1]] = 0;
存在重复计算的问题(因为外层的 $j 已经遍历了全部关键字节点)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

XREAL founder Xu Chi: Apple lets everyone see the future of spatial computing, and we work hard to let everyone see the present of spatial computing. XREAL founder Xu Chi: Apple lets everyone see the future of spatial computing, and we work hard to let everyone see the present of spatial computing. Jun 02, 2024 pm 07:55 PM

XREAL launched a new product - XREALBeamPro at the spatial computing new product launch conference, priced from 1,299 yuan. According to the official introduction, XREALBeamPro is a computing terminal that gradually releases 2D applications into 3D space. Equipped with XREALAR glasses, it will form a "complete consumer-grade AR space computing system" and minimize the migration cost of users from the mobile phone side. In terms of design, XREALBeamPro looks like a smartphone, but it is not a mobile phone product, but a spatial computing terminal equipped with a touch display. It is officially positioned as an “AR spatial computing terminal like a Phone”. At the press conference, XREAL founder and CEO Xu Chi expressed his enthusiasm for BeamPro’s capabilities.

How to build AR/VR applications using PHP How to build AR/VR applications using PHP Jun 11, 2023 pm 07:22 PM

With the development of AR (Augmented Reality) and VR (Virtual Reality), these two technologies have become an important part of innovation and experience. With the popularity of PHP as a network programming language, PHP has become a feasible choice for developing AR/VR applications. This article will introduce how to use PHP to build AR/VR applications. 1. Understand AR and VR technologies Before we start creating AR/VR applications, we need to understand the different technologies and libraries. AR technology allows virtual objects or information to interact with real-world environments. V

Microsoft explores AR/VR patents using microlens arrays for wide-range chief ray angle manipulation Microsoft explores AR/VR patents using microlens arrays for wide-range chief ray angle manipulation Sep 30, 2023 pm 09:29 PM

(Nweon September 26, 2023) Microsoft believes that MicroLED has the characteristics of small size, light weight, high brightness, and high packaging density, and may be particularly suitable for head-mounted devices that require high resolution, small size, and light weight. monitor. In a patent application titled "Microlenses providing wide range chief ray angle manipulation for panel display", Microsoft introduced a microlens that provides wide range chief ray angle manipulation for panel display, and a display system configured with the microlens array. Wherein, each microlens in the array corresponds to a respective pixel of the panel display. Microlenses are configured according to their

Mojie Technology debuted at CES for the first time, leading the AR innovation trend! Mojie Technology debuted at CES for the first time, leading the AR innovation trend! Jan 14, 2024 pm 09:06 PM

From January 9th to 12th, more than 4,000 exhibitors gathered at CES, the consumer electronics "Spring Festival Gala". As an innovative company in the field of consumer AR, Mojie Technology participated in CES for the first time, fully demonstrating the strong strength of Chinese AR companies in core devices, complete machine solutions and customized services. At the Mojie booth, the AR glasses based on binocular resin diffraction light waveguide + MicroLED were displayed, which aroused the interest of many visitors, who tried them on and experienced their functions. This AR glasses has many practical features. First, the wearer can view the text content in real time through the glasses lens, which makes reading more convenient. Secondly, the glasses have built-in microphones that can collect voice information and convert it into text in the specified language through the instant translation function.

NTT QONOQ Devices unveils Mirza wireless XR glasses for smartphones NTT QONOQ Devices unveils Mirza wireless XR glasses for smartphones Sep 09, 2024 pm 06:30 PM

NTT QONOQ Devices has unveiled the Mirza wireless XR glasses for smartphones, freeing users from needing to wrangle cords. The glasses can display virtual AR content in real-world spaces like Pokemon Go or their phone content on a large virtual displ

Christie: dual drive of technology + innovation brings unlimited possibilities Christie: dual drive of technology + innovation brings unlimited possibilities Apr 23, 2024 am 08:10 AM

As a technology company driven by innovation, Christie is able to provide comprehensive solutions, rich industry experience and a complete service network in intelligent audio-visual technology. At this year's InfoCommChina, Christie brought RGB pure laser projectors, 1DLP laser projectors, LED video walls, and content management and processing solutions. At the event site, a large-scale customized outer spherical dome specially designed for astronomical displays became the focus of the scene. Christie named it "Sphere Deep Space", and the Christie M4K25RGB pure laser projector gave it "green vitality" . Mr. Sheng Xiaoqiang, senior technical service manager of the Commercial Business Department in China, said: It is not difficult to realize an outer spherical dome projection, but it can be made smaller and the color

How to use the split method in Java String How to use the split method in Java String May 02, 2023 am 09:37 AM

The split method in String uses the split() method of String to split the String according to the incoming characters or strings and return the split array. 1. General usage When using general characters, such as @ or, as separators: Stringaddress="Shanghai@Shanghai City@Minhang District@Wuzhong Road";String[]splitAddr=address.split("@");System .out.println(splitAddr[0]+splitAddr[1]+splitAddr[2]+splitAddr[3

Is Apple Vision Pro strong? Foreign media people hesitated after two days of in-depth experience Is Apple Vision Pro strong? Foreign media people hesitated after two days of in-depth experience Feb 02, 2024 am 09:21 AM

This may be the most innovative product released by Apple in 2023, and it may also be the beginning of the next era for Apple. Apple Vision Pro will be officially launched on February 2, local time. What is the experience of using this product that caused a sensation when it was released last year? Recently, foreign technology editor Lance Ulanoff published an in-depth experience diary, which may allow us on the other side of the ocean to understand this product more intuitively and truly. CNMO also compiled Lance’s relevant diaries. Next, the "I" in the article refers to Lance himself. The article has been deleted, but it will not affect reading. Whatever you have heard or read

See all articles