仅替换字符串中的整个单词匹配项

发布于 2024-09-13 09:18:48 字数 332 浏览 1 评论 0原文

我想使用 php

示例替换完整的单词: 如果我有

$text = "Hello hellol hello, Helloz";

并且我使用

$newtext = str_replace("Hello",'NEW',$text);

新文本应该看起来像

新的你好1你好,你好

PHP 返回

新的你好1你好,NEWz

谢谢。

I would like to replace just complete words using php

Example :
If I have

$text = "Hello hellol hello, Helloz";

and I use

$newtext = str_replace("Hello",'NEW',$text);

The new text should look like

NEW hello1 hello, Helloz

PHP returns

NEW hello1 hello, NEWz

Thanks.

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

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

发布评论

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

评论(4

桃酥萝莉 2024-09-20 09:18:48

您想使用正则表达式。 \b 匹配单词边界。

$text = preg_replace('/\bHello\b/', 'NEW', $text);

如果 $text 包含 UTF-8 文本,则必须添加 Unicode 修饰符“u”,以便非拉丁字符不会被误解为单词边界:

$text = preg_replace('/\bHello\b/u', 'NEW', $text);

You want to use regular expressions. The \b matches a word boundary.

$text = preg_replace('/\bHello\b/', 'NEW', $text);

If $text contains UTF-8 text, you'll have to add the Unicode modifier "u", so that non-latin characters are not misinterpreted as word boundaries:

$text = preg_replace('/\bHello\b/u', 'NEW', $text);
征﹌骨岁月お 2024-09-20 09:18:48

字符串中的多个单词被此替换

    $String = 'Team Members are committed to delivering quality service for all buyers and sellers.';
    echo $String;
    echo "<br>";
    $String = preg_replace(array('/\bTeam\b/','/\bfor\b/','/\ball\b/'),array('Our','to','both'),$String);
    echo $String;
    Result: Our Members are committed to delivering quality service to both buyers and sellers.

multiple word in string replaced by this

    $String = 'Team Members are committed to delivering quality service for all buyers and sellers.';
    echo $String;
    echo "<br>";
    $String = preg_replace(array('/\bTeam\b/','/\bfor\b/','/\ball\b/'),array('Our','to','both'),$String);
    echo $String;
    Result: Our Members are committed to delivering quality service to both buyers and sellers.
听你说爱我 2024-09-20 09:18:48

数组替换列表:如果您的替换字符串相互替换,您需要 preg_replace_callback< /代码>

$pairs = ["one"=>"two", "two"=>"three", "three"=>"one"];

$r = preg_replace_callback(
    "/\w+/",                           # only match whole words
    function($m) use ($pairs) {
        if (isset($pairs[$m[0]])) {     # optional: strtolower
            return $pairs[$m[0]];      
        }
        else {
            return $m[0];              # keep unreplaced
        }
    },
    $source
);

显然/为了提高效率,/\w+/ 可以替换为键列表/\b(one|two|third)\b/i

Array replacement list: In case your replacement strings are substituting each other, you need preg_replace_callback.

$pairs = ["one"=>"two", "two"=>"three", "three"=>"one"];

$r = preg_replace_callback(
    "/\w+/",                           # only match whole words
    function($m) use ($pairs) {
        if (isset($pairs[$m[0]])) {     # optional: strtolower
            return $pairs[$m[0]];      
        }
        else {
            return $m[0];              # keep unreplaced
        }
    },
    $source
);

Obviously / for efficiency /\w+/ could be replaced with a key-list /\b(one|two|three)\b/i.

万人眼中万个我 2024-09-20 09:18:48

您还可以使用 T-Regx 库,该库引用 $\替换时的 字符

<?php
$text = pattern('\bHello\b')->replace($text)->all()->with('NEW');

You can also use T-Regx library, that quotes $ or \ characters while replacing

<?php
$text = pattern('\bHello\b')->replace($text)->all()->with('NEW');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文