字符串替换,怎么能做到双向替换
Jun 13, 2016 am 10:28 AM
字符串替换,如何能做到双向替换?
最近要做一个同义词替换的功能,因为是同义词,所以最好是能双向替换,比如“拜会”和“拜见”,在字符串中出现任何一个都要能替换成另一个。
通常用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>

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Sort array using Array.Sort function in C#

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

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

What are the string search and replace techniques in Python?

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
