简体   繁体   English

突出显示搜索结果的一部分

[英]Highlight parts of search results

I'm using the code below for highlight one word from file_get_content and go to anchor. 我正在使用以下代码突出显示file_get_content中的一个单词并定位。

$file='
IAR6=1002
SHF6=1
REF6=0002
TY7=2
DATE7=20130820182357
STAT_N7=1002
SEQ7=0002110000001
STA7=000005
TY8=2
DATE8=20130820182429
STAT_N8=1002
SH8=1
OP8=S123
SEQ8=0002120000081
';

$Seq = 0002110000001;
$text = preg_replace("/\b($Seq)\b/i", '<span class="highlight"><a name="here">\1</a></span>', $file);

for now this highlight : 0002110000001 现在这个亮点:0002110000001

i would like to highlight all part of the same index number. 我想突出显示同一索引号的所有部分。

ex: looking for 0002110000001 例如:寻找0002110000001

highlight this part of txt only where number is 7 仅在数字为7时突出显示txt的这一部分

TY7=2
DATE7=20130820182357
STAT_N7=1002
SEQ7=0002110000001
STA7=000005

Any help will be appreciated. 任何帮助将不胜感激。

EDIT: i try to be more specific. 编辑:我尝试更具体。 file contain lot of code parts always start by TYx (x is auto numbering) 文件包含很多代码部分,始终以TYx开头(x为自动编号)

i have the SEQ number for my search , in ex 0002110000001 the preg_replace("/\\b($Seq)\\b/i", '\\1 find 0002110000001 and higlight them. 我具有用于搜索的SEQ编号,在0002110000001中,preg_replace(“ / \\ b($ Seq)\\ b / i”,'\\ 1找到0002110000001并将其高亮显示。

what i need is higlight what is between TY7 and TY8 instead of only 0002110000001. 我需要的是TY7和TY8之间的高光,而不是只有0002110000001。

Hope this is clear enough due to my bad english 希望我的英语不好,这已经足够清楚了

thanks 谢谢

You can make use of stripos() and explode() in PHP 您可以在PHP中使用stripos()explode()

<?php
$file='
IAR6=1002
SHF6=1
REF6=0002
TY7=2
DATE7=20130820182357
STAT_N7=1002
SEQ7=0002110000001
STA7=000005
TY8=2
DATE8=20130820182429
STAT_N8=1002
SH8=1
OP8=S123
SEQ8=0002120000081
';
//$Seq = "0002110000001";
$Seq = "7";
$new_arr=explode(PHP_EOL,$file);
foreach($new_arr as $k=>$v)
{
if(stripos($v,$Seq)!==false)
{
echo "$v\n";
}
}

OUTPUT : 输出:

TY7=2
DATE7=20130820182357
STAT_N7=1002
SEQ7=0002110000001
STA7=000005

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM