php 字符串搜索,PHP: stripos - Manual

this would to work with any language, i hope.

tested on czech (eastern europe) lang.

*    SAFE HIGHLIGHT

****************************************/

/**

* function finds and encase every string in a $needleArr array with

* strings $shearLft (from the left side) and $shearRgt (guess from which

* side).

* already encased needles are IGNORED for any other step, so order

* of needles in $needleArr is pretty important.

*

* function is searching needles in case-insensitive mode,

* but case in the subject is saved.

*

* can you do it better? so, do it.

*

* @param array $needleArr array of needles

* @param string $shearLft left shear

* @param string $shearRgt right shear

* @param string $subject subject

* @param string $encoding encoding ('utf-8' is default)

*

* @author griffin

*/functionsafeHighlight($needleArr,$shearLft,$shearRgt,$subject,$encoding='utf-8')

{// encoding$e=$encoding;// oh, no needlesif( !is_array($needleArr))

return$subject;// empty keys throw-off, only unique, reindex$nA=array_values(array_unique(array_diff($needleArr, array(''))

)

);// needle countif( !($nC=count($nA)))

return$subject;// nothing to hl

// shear lengthif( !(($rLL=mb_strlen($rL=$shearLft,$e))

+ ($rRL=mb_strlen($rR=$shearRgt,$e))))

return$subject;// no shears

// subject lengthif( !($sL=mb_strlen($s=$subject,$e)))

returnnull;// empty subject

// subject in lowercase (we need to aviod

// using mb_stripos due to PHP version)$sW=mb_strtolower($s,$e);// masking ~ 0=not changed, 1=changed$m=str_repeat('0',$sL);// loop for each needlefor($n=0;$n

{// needle string loWercase$nW=mb_strtolower($nA[$n],$e);$o=0;// offset$nL=mb_strlen($nW,$e);// needle length

// search needlewhile(false!== ($p=mb_strpos($sW,$nW,$o,$e)))

{// oh hurrey, needle found on $p position

// is founded needle already modified? (in full-length)for($q=$p;$q

if($m[$q])

{// ai, caramba. already modified, jump over$o+=$nL;// continue for while() loop - not for for() loop!continue2;

}// explode subject and mask into three parts

// partA|needle|partB$sE[0] =mb_substr($s,0,$p,$e);$sE[1] =mb_substr($s,$p,$nL,$e);$sE[2] =mb_substr($s,$p+$nL,$sL-$p-$nL,$e);// mask

// partA|partB (needle not needed)$mE[0] =mb_substr($m,0,$p,$e);$mE[1] =mb_substr($m,$p+$nL,$sL-$p-$nL,$e);// apply shears$sE[1] =$rL.$sE[1].$rR;// update sunject length$sL+=$rLL+$rRL;// update mask$m=$mE[0] .str_repeat('1',$rLL+$nL+$rRL) .$mE[1];// implode into a subject$s=implode($sE);// update lowercase subject$sW=mb_strtolower($s,$e);// increase offset$o+=$rLL+$nL+$rRL;// end of string reachedif($o>=$sL)

break;

}// while()}// for( $n=0; $n

// oouu yeaaa, kick the subject out of the functionreturn$s;

}// function safeHighlight()

/****************************************

*    END: SAFE HIGHLIGHT

****************************************/?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值