用PHP方法替换某个;索引“;在一个字符串中(例如第三个字符)使用另一个字符串
说我讨厌“帽子”这个词。我想要一个类似于用PHP方法替换某个;索引“;在一个字符串中(例如第三个字符)使用另一个字符串,php,Php,说我讨厌“帽子”这个词。我想要一个类似于strreplace(“hat”,1,“o”)的函数,因此将其改为hot,而不是hat 有没有办法用函数来实现这一点?还是我必须自己写 function changeChar($string,$newchar,$pos){ $string[$pos] = $newchar; return $string; } echo changeChar("Logs","L",2); 会回应“LoLs” 会回应“LoLs” 您只需要str_replace(
strreplace(“hat”,1,“o”)
的函数,因此将其改为hot
,而不是hat
有没有办法用函数来实现这一点?还是我必须自己写
function changeChar($string,$newchar,$pos){
$string[$pos] = $newchar;
return $string;
}
echo changeChar("Logs","L",2);
会回应“LoLs”
会回应“LoLs”
您只需要str_replace()。请看这里: e、 g
您只需要str_replace()。请看这里: e、 g
你必须自己写:)
$foo='hat'$foo[1]=“o”代码>只适用于单个字符,不适用于字符串。substru replace是我要找的。谢谢。我不知道这个函数,很好,你找到了自己的答案。你必须自己写:)$foo='hat'$foo[1]=“o”代码>只适用于单个字符,不适用于字符串。substru replace是我要找的。谢谢。我不知道这个函数,很好,你找到了自己的答案。
Logs
0123 <position (g is the 2nd character ;)
$pos = $pos + 1;
<?php
function myReplacing($theString, $theCharacter, $thePosition)
{
return substr_replace($theString, $theCharacter, $thePosition, 1);
}
$a = "test";
$a = myReplacing($a, "u", 1);
echo $a;
?>
echo str_replace("a", "o", "hat");
// outputs 'hot'