gpt4 book ai didi

php - 突出显示字符串中的多个搜索词

转载 作者:行者123 更新时间:2023-11-29 03:26:27 25 4
gpt4 key购买 nike

在我的国家有一种叫做“Rut”的东西,就像每个人的身份证号码一样。车辙编号格式为“12.345.678-K”(K也可以是数字)。
因此,我制作了一个 ajax 实时搜索表单,以在表格中显示我数据库中的人员,当人们键入 12345678k 时,它会变成 12.345.678-k 并在数据库中搜索 Rut($q 是搜索词) ).

SELECT * FROM clients WHERE rut LIKE '%$ruts%' OR rut LIKE '%$ruts2%' OR rut LIKE '%$q%'


格式:

//Rut format
if(strlen($q)==3){
// 123 -> 12.3
$ruts = substr_replace($q, '.', 2, -1);
}elseif(strlen($q)==4){
// 1234 -> 12.34
$ruts = substr_replace($q, '.', 2, -2);
}elseif(strlen($q)==5){
// 12345 -> 12.345
$ruts = substr_replace($q, '.', 2, -3);
}elseif(strlen($q)==6){
// 123456 -> 12.3456
// 12.3456 -> 12.345.6
$ruta = substr_replace($q, '.', 2, -4);
$ruts = substr_replace($ruta, '.', 6, -1);
}elseif(strlen($q)==7){
// 1234567 -> 12.34567
// 12.34567 -> 12.345.67
$ruta = substr_replace($q, '.', 2, -5);
$ruts = substr_replace($ruta, '.', 6, -2);
}elseif(strlen($q)==8){
// 12345678 -> 12.345.678
// 12.345678 -> 12.345.678
$ruta = substr_replace($q, '.', 2, -6);
$ruts = substr_replace($ruta, '.', 6, -3);
}elseif(strlen($q)==9){
// 12345678k -> 12.345678k
// 12.345678k -> 12.345.678k
// 12.345.678k -> 12.345.678-k
$ruta = substr_replace($q, '.', 2, -7);
$rutb = substr_replace($ruta, '.', 6, -4);
$ruts = substr_replace($rutb, '-', 10, -1);
}else{
$ruts = $q;
}


我还创建了 str_replace(),以在我输入时突出显示结果。

$highlightrut = str_replace("$ruts", "<span style='color:red'>$ruts</span>", $output['rut']);

问题是,还有另一种不同的Rut格式(1.234.567-K),我已经做了格式转换

//Rut format 2 (1.234.567-K)
if(strlen($q)==2){
// 12 -> 1.2
$ruts2 = substr_replace($q, '.', 1, -1);
}elseif(strlen($q)==3){
// 123 -> 1.23
$ruts2 = substr_replace($q, '.', 1, -2);
}elseif(strlen($q)==4){
// 1234 -> 1.234
$ruts2 = substr_replace($q, '.', 1, -3);
}elseif(strlen($q)==5){
// 12345 -> 1.2345
// 1.2345 -> 1.234.5
$ruta2 = substr_replace($q, '.', 1, -4);
$ruts2 = substr_replace($ruta2, '.', 5, -1);
}elseif(strlen($q)==6){
// 123456 -> 1.23456
// 1.23456 -> 1.234.56
$ruta2 = substr_replace($q, '.', 1, -5);
$ruts2 = substr_replace($ruta2, '.', 5, -2);
}elseif(strlen($q)==7){
// 1234567 -> 1.234567
// 1.234567 -> 1.234.567
$ruta2 = substr_replace($q, '.', 1, -6);
$ruts2 = substr_replace($ruta2, '.', 5, -3);
}elseif(strlen($q)==8){
// 1234567k -> 1.234567k
// 1.234567k -> 1.234.567k
// 1.234.567k -> 1.234.567-k
$ruta2 = substr_replace($q, '.', 1, -7);
$rutb2 = substr_replace($ruta2, '.', 5, -4);
$ruts2 = substr_replace($rutb2, '-', 9, -1);
}else{
$ruts2 = $q;
}

但我不知道如何在同一个字符串中突出显示结果。

最佳答案

我解决了,谢谢大家的支持。
这是代码:

if(preg_match('~('.$ruts.')~', $output['rut'])) {
$highlightrut = str_replace("$ruts", "<span style='color:red'>$ruts</span>", $output['rut']);
}elseif(preg_match('~('.$ruts2.')~', $output['rut'])) {
$highlightrut =str_replace("$ruts2", "<span style='color:red'>$ruts2</span>", $output['rut']);
}

关于php - 突出显示字符串中的多个搜索词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35485524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com