PHP字符串替换匹配整个单词

时间:2021-09-15 19:25:57

I would like to replace just complete words using php

我想用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

新hello1你好,Helloz

PHP returns

PHP返回

NEW hello1 hello, NEWz

新hello1你好,NEWz

Thanks.

谢谢。

3 个解决方案

#1


51  

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

您需要使用正则表达式。\b匹配一个单词边界。

$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包含UTF-8文本,则必须添加Unicode修饰符“u”,以免将非拉丁字符误解为单词边界:

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

#2


4  

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;

#3


1  

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

数组替换列表:如果替换字符串相互替换,则需要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.

显然/为了效率/\w+/可以用一个键表/\b(一个|两个|三个)\b/i代替。

#1


51  

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

您需要使用正则表达式。\b匹配一个单词边界。

$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包含UTF-8文本,则必须添加Unicode修饰符“u”,以免将非拉丁字符误解为单词边界:

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

#2


4  

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;

#3


1  

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

数组替换列表:如果替换字符串相互替换,则需要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.

显然/为了效率/\w+/可以用一个键表/\b(一个|两个|三个)\b/i代替。