Home  >  Article  >  Backend Development  >  如何来对字符串进行替换

如何来对字符串进行替换

WBOY
WBOYOriginal
2016-06-13 13:46:17855browse

怎么来对字符串进行替换?
$a="1234683214312354436214214125436121436";
$b="76234265832465867892353463476254273434252";

求把$a中的最中间的那个1替换成'@'; 比如有11个1 把第6个替换掉
同样求把$b中最中间那个2替换成'@'; 比如有8个2 把第4个替换掉

------解决方案--------------------
利用preg_match_all + PREG_OFFSET_CAPTURE就可以匹配到你想替换的东西,以及匹配的东西的偏移量,接下来怎么做你你懂的。
$a="1234683214312354436214214125436121436";
$b="76234265832465867892353463476254273434252";
preg_match_all('#1#',$a,$match,PREG_OFFSET_CAPTURE);
echo "

";                                                                                                                                              
print_r($match);
?>
------解决方案--------------------
探讨

引用:
不出现两次报错的问题我修正了


PHP code

function fool($str, $number, $change)
{

$array = str_split($str);
if (in_array($number, $array)) {
$key = array_keys($array, $number);
$……
Statement:
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