dongxie2756 2014-05-08 22:19
浏览 483

找到数组中最长的匹配字符串

I have an array of hierarchically arranged identifiers (SNMP sysObjectIDs), that I'd like to match against in order to find the closest match.

For example, if my array contains :

.1.3.6.1.4.1.207         = alliedware
.1.3.6.1.4.1.207.1.14    = alliedwareplus
.1.3.6.1.4.1.207.1.4.126 = allied-radlan
.1.3.6.1.4.1.207.1.4.125 = allied-radlan

And I search for

.1.3.6.1.4.1.207.1.14.69

I would like it to return the alliedwareplus entry.

If I search for

.1.3.6.1.4.1.207.1.4

It should return the alliedware entry.

Basically I just want to return the longest match starting from the beginning of the string.

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • doucepei5298 2014-05-08 22:23
    关注
    1. Sort the array by the number of components in the object ID, from high to low.
    2. Loop through the array, testing whether the object ID in the array is a prefix of the input object ID.
    3. When you find a match like this, break out of the loop.

    All these steps will probably be easiest if you first convert all the object IDs to an array:

    $objid_arr = explode('.', $objid);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献