strpos()与多针? [英] strpos() with multiple needles?

查看:133
本文介绍了strpos()与多针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找像strpos()这样的函数,它有两个显着的区别:


  1. 为了能够接受多个针头。我的意思是数以千计的针头。

  2. 在干草堆中搜索所有出现的针,并返回一组起始位置。

当然,它必须是一个有效的解决方案,不仅仅是每个针头的循环。我已经搜遍了这个论坛,并且有类似的问题,比如:



但是他们不知道我在找什么。我使用strpos只是为了更好地说明我的问题,或许完全不同的是为了这个目的而使用的。



我知道 Zend_Search_Lucene ,我很感兴趣,如果它可以用来实现这一点,以及如何(只是一般的想法)



非常感谢您的帮助和时间!

>以下是我的策略示例代码:

 函数strpos_array($ haystack,$ needles,$ offset = 0){
$ matches = array();

//避免显而易见:当干草堆或针头为空时,返回不匹配
if(empty($ needles)|| empty($ haystack)){
return $火柴;
}

$ haystack =(string)$ haystack; //预制非字符串干草堆
$ haylen = strlen($ haystack);

//如果($ offset< 0){
$ offset + = $ heylen; //允许负数(从干草堆的末端)抵消
;


//如果没有阵列或只有一根针,请使用strpos
if(!is_array($ needles)){
$ needles = array($针);
}

$ needles = array_unique($ needles); //如果您确定所有针都是唯一的,则不需要

//预先计算针长以节省时间
foreach($ needles as& $ origNeedle){
$ origNeedle =数组((字符串)$ origNeedle,strlen($ origNeedle)); ($; $ offset $ lt; $ haylen; $ offset ++){
foreach($ needles as $ needle){

$ //查找匹配
$ b list($ needle,$ length)= $ needle;
if($ needle == substr($ haystack,$ offset,$ length)){
$ matches [] = $ offset;
休息;
}
}
}

return($ matches);
}

我已经实现了一个简单的暴力方法,可以与任何组合针和干草堆(不只是文字)。对于可能更快的算法检查出来:








其他解决方案

 函数strpos_array($ haystack,$ needles,$ theOffset = 0 ){
$ matches = array();

if(empty($ haystack)|| empty($ needles)){
return $ matches;
}

$ haylen = strlen($ haystack);

if($ theOffset< 0){//支持负偏移
$ theOffest + = $ haylen;
}

foreach($ needles as $ needle){
$ needlelen = strlen($ needle);
$ offset = $ theOffset; ($ match = strpos($ haystack,$ needle,$ offset))!== == false){
$ matches [] = $ match;
$ b $
$ offset = $ match + $ needlelen;
if($ offset> = $ haylen){
break;
}
}
}

return $ matches;
}


I am looking for a function like strpos() with two significant differences:

  1. To be able to accept multiple needles. I mean thousands of needles at ones.
  2. To search for all occurrences of the needles in the haystack and to return an array of starting positions.

Of course it has to be an efficient solution not just a loop through every needle. I have searched through this forum and there were similar questions to this one, like:

but nether of them was what I am looking for. I am using strpos just to illustrate my question better, probably something entirely different has to be used for this purpose.

I am aware of Zend_Search_Lucene and I am interested if it can be used to achieve this and how (just the general idea)?

Thanks a lot for Your help and time!

解决方案

Here's some sample code for my strategy:

function strpos_array($haystack, $needles, $offset=0) {
    $matches = array();

    //Avoid the obvious: when haystack or needles are empty, return no matches
    if(empty($needles) || empty($haystack)) {
        return $matches;
    }

    $haystack = (string)$haystack; //Pre-cast non-string haystacks
    $haylen = strlen($haystack);

    //Allow negative (from end of haystack) offsets
    if($offset < 0) {
        $offset += $heylen;
    }

    //Use strpos if there is no array or only one needle
    if(!is_array($needles)) {
        $needles = array($needles);
    }

    $needles = array_unique($needles); //Not necessary if you are sure all needles are unique

    //Precalculate needle lengths to save time
    foreach($needles as &$origNeedle) {
        $origNeedle = array((string)$origNeedle, strlen($origNeedle));
    }

    //Find matches
    for(; $offset < $haylen; $offset++) {
        foreach($needles as $needle) {
            list($needle, $length) = $needle;
            if($needle == substr($haystack, $offset, $length)) {
                $matches[] = $offset;
                break;
            }
        }
    }

    return($matches);
}

I've implemented a simple brute force method above that will work with any combination of needles and haystacks (not just words). For possibly faster algorithms check out:


Other Solution

function strpos_array($haystack, $needles, $theOffset=0) {
    $matches = array();

    if(empty($haystack) || empty($needles)) {
        return $matches;
    }

    $haylen = strlen($haystack);

    if($theOffset < 0) {  // Support negative offsets
        $theOffest += $haylen;
    }

    foreach($needles as $needle) {
        $needlelen = strlen($needle);
        $offset = $theOffset;

        while(($match = strpos($haystack, $needle, $offset)) !== false) {
            $matches[] = $match;
            $offset = $match + $needlelen;
            if($offset >= $haylen) {
                break;
            }
        }
    }

    return $matches;
}

这篇关于strpos()与多针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆