如果包含或匹配搜索词,则将整个单词和单个字符包含在 HTML 标记中

发布于 2024-09-16 03:03:41 字数 774 浏览 7 评论 0原文

我想将 HTML 标签添加到包含搜索词的单词中,并且我想在每次出现的搜索词周围添加嵌套标签。

示例字符串:

$str = "i like programming very much";

示例搜索词:

$word = "r";

所需结果:

i like <span class='pice1'>p<span class='pice2'>r</span>og<span class='pice2'>r</span>aming</span> <span class='pice1'>ve<span class='pice2'>r</span>y</span> much

我为其编写了以下正则表达式,但有时不起作用。

$str = preg_replace(
    "/([^\s{" . preg_quote($word) . "}]*?)(" . preg_quote($word) . ")([^\s{" . preg_quote($word) . "}]*)/siu",
    "<span class='pice1'>$1</span><span class='pice2'>$2</span><span class='pice1'>$3</span>",
    $str
);

I want to add HTML tags to words that contain a search term AND I want to add nested tags around each occurrence of the search term.

Sample string:

$str = "i like programming very much";

Sample search term:

$word = "r";

Desired result:

i like <span class='pice1'>p<span class='pice2'>r</span>og<span class='pice2'>r</span>aming</span> <span class='pice1'>ve<span class='pice2'>r</span>y</span> much

I wrote the following regex for it, but it sometimes doesn't work.

$str = preg_replace(
    "/([^\s{" . preg_quote($word) . "}]*?)(" . preg_quote($word) . ")([^\s{" . preg_quote($word) . "}]*)/siu",
    "<span class='pice1'>$1</span><span class='pice2'>$2</span><span class='pice1'>$3</span>",
    $str
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

幸福丶如此 2024-09-23 03:03:41

为什么不直接使用 str_replace() 呢?我认为这更简单

$search = "ab";
$word = "ameriabank";
$newstr = "<span class=\"pice1\">".str_replace($search, $word, "</span><span class=\"pice3\">".$search."</span></span class=\"pice1>\")."</span>";

Why dont't you just use str_replace()? I think it's more simple

$search = "ab";
$word = "ameriabank";
$newstr = "<span class=\"pice1\">".str_replace($search, $word, "</span><span class=\"pice3\">".$search."</span></span class=\"pice1>\")."</span>";
近箐 2024-09-23 03:03:41
$str = "i like programming very much";
$w = "r";
echo preg_replace("/($w)/", "<b>$1</b>", $str);

输出:

i like p<b>r</b>og<b>r</b>amming ve<b>r</b>y much

回答评论:分两步完成。

$str = "i like programming very much ready tear";
$w = "r";
$str = preg_replace("/\\b((?:\\w+|\\b)$w(\\w+|\\b))\\b/", "<i>$1</i>", $str);
$str = preg_replace("/($w)/", "<b>$1</b>", $str);
echo $str;

输出:

i like <i>p<b>r</b>og<b>r</b>amming</i> <i>ve<b>r</b>y</i> much <i><b>r</b>eady</i> <i>tea<b>r</b></i>
$str = "i like programming very much";
$w = "r";
echo preg_replace("/($w)/", "<b>$1</b>", $str);

Output:

i like p<b>r</b>og<b>r</b>amming ve<b>r</b>y much

Answer to the comment: do it in two steps.

$str = "i like programming very much ready tear";
$w = "r";
$str = preg_replace("/\\b((?:\\w+|\\b)$w(\\w+|\\b))\\b/", "<i>$1</i>", $str);
$str = preg_replace("/($w)/", "<b>$1</b>", $str);
echo $str;

output:

i like <i>p<b>r</b>og<b>r</b>amming</i> <i>ve<b>r</b>y</i> much <i><b>r</b>eady</i> <i>tea<b>r</b></i>
骄傲 2024-09-23 03:03:41

这种方式怎么样:

$str = "i like programming very much";
$word = "r";
$list = explode(' ',$str);
for($i=0; $i<count($list); $i++) {
    if(preg_match("/$word/", $list[$i])) {
        $list[$i] = '<i>'.preg_replace("/$word/siu", "<b>$word</b>", $list[$i]).'</i>';
    }
}
$str = implode(' ',$list);
echo $str,"\n";

What about this way :

$str = "i like programming very much";
$word = "r";
$list = explode(' ',$str);
for($i=0; $i<count($list); $i++) {
    if(preg_match("/$word/", $list[$i])) {
        $list[$i] = '<i>'.preg_replace("/$word/siu", "<b>$word</b>", $list[$i]).'</i>';
    }
}
$str = implode(' ',$list);
echo $str,"\n";
只想待在家 2024-09-23 03:03:41
$str = "i like programming very much";
$word = "r";
function highlight($matches)
{
    global $word;
    return '<span class="pice1">'.str_replace($word,'<span class="pice2">'.$word.'</span>',$matches[0]).'</span>';
}
echo $str = preg_replace_callback("/([^\s]*?".preg_quote($word, '/')."[^\s]*)/siu", highlight, $str);

完成这项工作(它也适用于外语)...

$str = "i like programming very much";
$word = "r";
function highlight($matches)
{
    global $word;
    return '<span class="pice1">'.str_replace($word,'<span class="pice2">'.$word.'</span>',$matches[0]).'</span>';
}
echo $str = preg_replace_callback("/([^\s]*?".preg_quote($word, '/')."[^\s]*)/siu", highlight, $str);

do the job(and it works with foreign languages too)...

那小子欠揍 2024-09-23 03:03:41

要将包含搜索词的整个单词包装在给定 HTML 标记中,请使用 preg_replace_callback() 并使正则表达式模式不区分大小写且以 unicode 安全方式匹配搜索词前后的零个或多个单词字符。

隔离整个单词后,再次搜索该搜索词,并用另一个 HTML 标签包裹每个出现的位置。 str_replace() 不适合这种内部替换,因为它不会保留匹配搜索词的大小写。

代码:(演示)

<style>
    .whole_word {color: blue;}
    .sub_word {color: red;}
</style>

<?php

$str = 'i like programming veRy much';
$find = 'r';
$safeFind = preg_quote($find);
$wordStart = '<span class="whole_word">';
$wordEnd = '</span>';
$partStart = '<span class="sub_word">';
$partEnd = '</span>';

echo preg_replace_callback(
    "#\w*{$safeFind}\w*#iu",
    fn($m) => $wordStart . preg_replace("#{$safeFind}#iu", $partStart . '\0' . $partEnd, $m[0]) . $wordEnd,
    $str
);

原始输出:

i like <span class="whole_word">p<span class="sub_word">r</span>og<span class="sub_word">r</span>amming</span> <span class="whole_word">ve<span class="sub_word">R</span>y</span> much

渲染的 HTML

在此处输入图像描述

To wrap whole words which contain the search term in a given HTML tag, use preg_replace_callback() and have the regex pattern case-insensitively and unicode-safely match zero or more word character before and after the search term.

Once the whole word is isolated, search for the search term again and wrap each occurrence with another HTML tag. str_replace() is not suitable for this inner replacement because it will not preserve the case of the matched search term.

Code: (Demo)

<style>
    .whole_word {color: blue;}
    .sub_word {color: red;}
</style>

<?php

$str = 'i like programming veRy much';
$find = 'r';
$safeFind = preg_quote($find);
$wordStart = '<span class="whole_word">';
$wordEnd = '</span>';
$partStart = '<span class="sub_word">';
$partEnd = '</span>';

echo preg_replace_callback(
    "#\w*{$safeFind}\w*#iu",
    fn($m) => $wordStart . preg_replace("#{$safeFind}#iu", $partStart . '\0' . $partEnd, $m[0]) . $wordEnd,
    $str
);

Raw Output:

i like <span class="whole_word">p<span class="sub_word">r</span>og<span class="sub_word">r</span>amming</span> <span class="whole_word">ve<span class="sub_word">R</span>y</span> much

Rendered HTML

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文