亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP實(shí)現(xiàn)的字符串匹配算法示例【sunday算法】

 更新時(shí)間:2017年12月19日 09:57:35   作者:飛蓬058  
這篇文章主要介紹了PHP實(shí)現(xiàn)的字符串匹配算法,簡(jiǎn)單描述了sunday算法的概念與原理,并結(jié)合實(shí)例形式分析了php基于sunday算法實(shí)現(xiàn)字符串匹配操作相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP實(shí)現(xiàn)的字符串匹配算法————sunday算法。分享給大家供大家參考,具體如下:

Sunday算法是Daniel M.Sunday于1990年提出的字符串模式匹配。其核心思想是:在匹配過(guò)程中,模式串發(fā)現(xiàn)不匹配時(shí),算法能跳過(guò)盡可能多的字符以進(jìn)行下一步的匹配,從而提高了匹配效率。

<?php
/*
 *@param $pattern 模式串
 *@param $text 待匹配串
 */
function mySunday($pattern = '',$text = ''){
  if(!$pattern || !$text) return false;
  $pattern_len = mb_strlen($pattern);
  $text_len = mb_strlen($text);
  if($pattern_len >= $text_len) return false;
  $i = 0;
  for($i = 0; $i < $pattern_len; $i++){ //組裝以pattern中的字符為下標(biāo)的數(shù)組
    $shift[$pattern[$i]] = $pattern_len - $i;
  }
  while($i <= $text_len - $pattern_len){
    $nums = 0;   //匹配上的字符個(gè)數(shù)
    while($pattern[$nums] == $text[$i + $nums]){
      $nums++;
      if($nums == $pattern_len){
        return "The first match index is $i\n";
      }
    }
    if($i + $pattern_len < $text_len && isset($shift[$text[$i + $pattern_len]])){ //判斷模式串后一位字符是否在模式串中
      $i += $shift[$text[$i + $pattern_len]];   //對(duì)齊該字符
    }else{
      $i += $pattern_len;   //直接滑動(dòng)pattern_len位
    }
  }
}
$text = "I am testing mySunday on sunday!";
$pattern = "sunday";
echo mySunday($pattern,$text);

運(yùn)行結(jié)果:

The first match index is 25 

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》及《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論