如何在PHP中同时替换2个字符串(使用eachother) [英] How to replace 2 strings (with eachother) simultaneously in PHP

查看:103
本文介绍了如何在PHP中同时替换2个字符串(使用eachother)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的事情非常简单,但我期待最有效地做到这一点,最好使用php builtin fns。

What I'm trying to do is very simple, but I'm looking to do it most efficiently, preferably using php builtin fns.

$str = '1234';
echo replace_function(array('1','3'),array('3','1'),$str);

// output: 3214

str_replace,preg_replace会导致1214,这意味着它会遍历数组,替换匹配的字符串。我正在寻找一种解决方案,以同时切换这两个(或更多)字符串。

str_replace,preg_replace would result in 1214, which means it goes through the arrays, replacing matched strings. I'm looking for a solution to simultaneously "switch" these two (or more) strings.

有什么想法?

any ideas?

推荐答案

您需要字符串翻译:
http://php.net/manual/en/function.strtr.php

<?php
$trans = array("hello" => "hi", "hi" => "hello");
echo strtr("hi all, I said hello", $trans);
// = hello all, I said hi
?> 

这篇关于如何在PHP中同时替换2个字符串(使用eachother)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆