Home Backend Development PHP Tutorial 字符串替换,怎么能做到双向替换

字符串替换,怎么能做到双向替换

Jun 13, 2016 am 10:28 AM
array find quot replace str

字符串替换,如何能做到双向替换?
最近要做一个同义词替换的功能,因为是同义词,所以最好是能双向替换,比如“拜会”和“拜见”,在字符串中出现任何一个都要能替换成另一个。

通常用str_replace只能单向替换,不能实现双向替换。

尝试了一下把同义词做成数组再用str_replace替换,结果却不正确。

$find = array("拜会","拜见");
$replace = array("拜见","拜会");
$arr = array("拜会和拜见");
str_replace($find,$replace,$arr);

结果是:拜会和拜会。

问题是怎么样才能使最后的替换结果是“拜见和拜会”?


------解决方案--------------------
改动一下

PHP code
$find = array("拜会","拜见");$replace = array("拜见","拜会");$str = "拜会和拜见";echo    strtr($str, array_combine($find, $replace));<div class="clear">
                 
              
              
        
            </div>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Sort array using Array.Sort function in C#

OPPO Find X7 is a masterpiece! Capture your every moment with images OPPO Find X7 is a masterpiece! Capture your every moment with images Aug 07, 2024 pm 07:19 PM

OPPO Find X7 is a masterpiece! Capture your every moment with images

How to use the REPLACE function to replace a specified part of a string in MySQL How to use the REPLACE function to replace a specified part of a string in MySQL Jul 25, 2023 pm 01:18 PM

How to use the REPLACE function to replace a specified part of a string in MySQL

OPPO Find X8 Ultra core configuration exposed! Snapdragon 8 Gen4+ Extra Large Battery OPPO Find X8 Ultra core configuration exposed! Snapdragon 8 Gen4+ Extra Large Battery Aug 22, 2024 pm 06:54 PM

OPPO Find X8 Ultra core configuration exposed! Snapdragon 8 Gen4+ Extra Large Battery

What are the string search and replace techniques in Python? What are the string search and replace techniques in Python? Oct 20, 2023 am 11:42 AM

What are the string search and replace techniques in Python?

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

Simple and clear method to use PHP array_merge_recursive() function

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

How to use the array_combine function in PHP to combine two arrays into an associative array

See all articles