Home > Backend Development > PHP Tutorial > 字符串 轮换

字符串 轮换

WBOY
Release: 2016-06-13 12:00:41
Original
1026 people have browsed it

字符串 替换
1要替换成apple,2要替换成pear,3要换成banana,4要换成orange。

这样的话,从数据库里读出来是23  。结果是pearbanana。
1234 ===applepearbananaorange。

就是要写一个这样的函数,根据数据查出来的数据换成水果名。

简洁的函数最好,不胜感激
------解决方案--------------------
不需要写,php 已经提供了

$d = array( 1 => 'apple', 2 => 'pear', 3 => 'banana', 4 => 'orange');<br />echo strtr('1234',$d);
Copy after login
applepearbananaorange

------解决方案--------------------
$a = array(1,2,3,4);<br />$b   = array('apple','pear','banana','orange');<br />$output  = str_replace($a, $b, '1234');<br />echo $output;
Copy after login

------解决方案--------------------
<br />$str = '1234';<br /><br />echo change($str);<br /><br />function change($str){<br />    $name = array('1'=>'apple', '2'=>'pear', '3'=>'banana', '4'=>'orange');    <br />    $tmp = '';<br />    for($i=0,$len=strlen($str); $i<$len; $i++){<br />        $tmp .= $name[substr($str, $i, 1)];<br />    }<br />    return $tmp;<br />}<br />
Copy after login

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template