不区分大小写 preg_replace_callback

发布于 2024-09-30 04:08:26 字数 796 浏览 4 评论 0原文

在下面的函数中,我想匹配关键字不区分大小写(应匹配“Blue Yoga Mats”和“blue Yoga mats”)...

但是,目前仅在关键字大小写相同时才匹配。

$mykeyword = "蓝色瑜伽垫";

$post->post_content = preg_replace_callback("/\b($mykeyword)\b/","doReplace", $post->post_content);

// the callback function
function doReplace($matches)
{
    static $count = 0;

    // switch on $count and later increment $count.
    switch($count++) {
        case 0: return '<b>'.$matches[1].'</b>';   // 1st instance, wrap in bold
        case 1: return '<em>'.$matches[1].'</em>'; // 2nd instance, wrap in italics
        case 2: return '<u>'.$matches[1].'</u>'; // 3rd instance, wrap in underline
        default: return $matches[1];              // don't change others.
            }
    }

In the function below, I want to match the keyword case insensitive (should match "Blue Yoga Mats" and "blue yoga mats")...

However, it currently only matches if the keyword is the same case.

$mykeyword = "Blue Yoga Mats";

$post->post_content = preg_replace_callback("/\b($mykeyword)\b/","doReplace", $post->post_content);

// the callback function
function doReplace($matches)
{
    static $count = 0;

    // switch on $count and later increment $count.
    switch($count++) {
        case 0: return '<b>'.$matches[1].'</b>';   // 1st instance, wrap in bold
        case 1: return '<em>'.$matches[1].'</em>'; // 2nd instance, wrap in italics
        case 2: return '<u>'.$matches[1].'</u>'; // 3rd instance, wrap in underline
        default: return $matches[1];              // don't change others.
            }
    }

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

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

发布评论

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

评论(5

路还长,别太狂 2024-10-07 04:08:26

只需将 i 修饰符添加到您的正则表达式中即可使其执行不区分大小写的匹配:

"/\b($mykeyword)\b/i"

顺便说一句,如果您还没有这样做,您需要从关键字中转义特殊的正则表达式字符。如果存在任何一个,它们可能会搞乱你的正则表达式并导致 PHP 警告/错误。在执行之前调用 preg_quote()替代品:

$mykeyword_escaped = preg_quote($mykeyword, '/');
$post->post_content = preg_replace_callback("/\b($mykeyword_escaped)\b/i","doReplace", $post->post_content);

Simply add the i modifier to your regex to make it perform a case insensitive match:

"/\b($mykeyword)\b/i"

By the way, if you haven't already, you need to escape special regex characters from your keyword. In case any are present, they could screw up your regex and cause PHP warnings/errors. Call preg_quote() before you perform the replacement:

$mykeyword_escaped = preg_quote($mykeyword, '/');
$post->post_content = preg_replace_callback("/\b($mykeyword_escaped)\b/i","doReplace", $post->post_content);
瞄了个咪的 2024-10-07 04:08:26

将“i”修饰符添加到您的正则表达式中:

/\b($mykeyword)\b/i

Add the "i" modifier to your regexp:

/\b($mykeyword)\b/i
对你的占有欲 2024-10-07 04:08:26
$post->post_content = preg_replace_callback("/\b($mykeyword)\b/i","doReplace", $post->post_content);

使用 TOKENregexpTOKENi 执行不区分大小写的搜索。

有关完整详细信息,请参阅 PHP 手册中的模式修饰符关于修饰符。

$post->post_content = preg_replace_callback("/\b($mykeyword)\b/i","doReplace", $post->post_content);

Use TOKENregexpTOKENi to perform case-insensitive searches.

See Pattern Modifiers in the PHP manual for full details on modifiers.

倾`听者〃 2024-10-07 04:08:26

使用 /i 修饰符:

$post->post_content = preg_replace_callback("/\b($mykeyword)\b/i","doReplace", $post->post_content);

Use the /i modifier:

$post->post_content = preg_replace_callback("/\b($mykeyword)\b/i","doReplace", $post->post_content);
如此安好 2024-10-07 04:08:26

您还可以使用 T-Regx 库

<?php
pattern('\b($mykeyword)\b')->replace($post->post_content)->callback('doReplace');
      // ↑ Delimiters are not required 

此外,使用 $mykeyword 可能会导致用户输入字符来打破您的模式。通过T-Regx,您可以使用准备好的模式,然后构建你的模式:

<?php
$pattern = Pattern::inject("\b(@keyword)\b", [
    'keyword' => $mykeyword  
    // quoting unsafe characters
]);
$pattern->replace($post->post_content)->callback('doReplace');

You can also use T-Regx library:

<?php
pattern('\b($mykeyword)\b')->replace($post->post_content)->callback('doReplace');
      // ↑ Delimiters are not required 

Also, use of $mykeyword might cause user-input characters to break your pattern. With T-Regx you can use Prepared Patterns and just build your pattern:

<?php
$pattern = Pattern::inject("\b(@keyword)\b", [
    'keyword' => $mykeyword  
    // quoting unsafe characters
]);
$pattern->replace($post->post_content)->callback('doReplace');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文