Php 字符串替换而不使用任何内置函数
使用程序替换字符串中的子字符串,而不使用str_replace 应为通用功能,应适用于以下示例:Php 字符串替换而不使用任何内置函数,php,arrays,string,algorithm,Php,Arrays,String,Algorithm,使用程序替换字符串中的子字符串,而不使用str_replace 应为通用功能,应适用于以下示例: 世界你好 替换单词:llo 替换为:zz 输出应为:Hezz world 世界你好 替换单词:o 替换为:xx 输出应为:Hellxx wxxrld 这就是我为解决这个问题而写的 function stringreplace($str, $stringtoreplace, $stringreplaceby){ $i=0;$add=''; while($str[$i] != ''){
function stringreplace($str, $stringtoreplace, $stringreplaceby){
$i=0;$add='';
while($str[$i] != ''){
$add .= $str[$i];
$j=0;$m=$i;$l=$i;$check=0;
if($str[$i] == $stringtoreplace[$j]){
while($stringtoreplace[$j] != ''){
if($str[$m] == $stringtoreplace[$j]){
$check++;
}
$j++;$m++;
}
if($check == strlen($stringtoreplace)){
$n=0;$sub='';
for($n=0;$n<=strlen($stringtoreplace);$n++){
$str[$l] = '';
$sub .= $str[$l];
$l++;
}
$add .= $stringreplaceby;
$i += $check;
}
}
$i++;
}//echo $add;exit;
return $add;
}
函数stringreplace($str、$stringtoreplace、$stringreplaceby){
$i=0;$add='';
而($str[$i]!=''){
$add.=$str[$i];
$j=0;$m=i;$l=i;$check=0;
如果($str[$i]=$stringtoreplace[$j]){
而($stringtoreplace[$j]!=''){
如果($str[$m]=$stringtoreplace[$j]){
$check++;
}
$j++;$m++;
}
如果($check==strlen($stringtoreplace)){
$n=0;$sub='';
对于($n=0;$n尝试这个最简单的方法,不使用str\u replace
,这里我们使用explode
和infrade
和substr\u count
1.子字符串计数
用于计数和检查子字符串是否存在
2.explode
用于根据匹配的子字符串将字符串分解为数组
3.内爆
用替换连接字符串
您可以通过将主字符串分解为数组,然后将stringreplaceby内爆为数组部分来创建新字符串
<?php
function stringreplce($str,$strreplace,$strreplaceby){
$str_array=explode($strreplace,$str);
$newstr=implode($strreplaceby,$str_array);
return $newstr;
}
echo stringreplce("Hello World","llo","zz")."<br>";
echo stringreplce("Hello World","Hel","zz")."<br>";
echo stringreplce("Hello World"," Wo","zz")."<br>";
echo stringreplce("Hello World","rl","zz")."<br>";
echo stringreplce("Hello World","ld","zz")."<br>";
?>
函数甘蒂加塔($kata,$kata\u awal,$kata\u甘蒂){
$i=0;$hasil;
$panjang=strlen($kata);
而($i<$panjang){
如果($kata[$i]=$kata_awal){
$hasil[$i]=$kata_ganti;
echo$hasil[$i];
}
否则如果($kata[$i]!==$kata_awal){
$hasil[$i]=$kata[$i];
echo$hasil[$i];
}
$i++;
}
}
echo ganti_kata(‘Riowaldy Indrawan’、‘a’、‘x’);
这段代码使用php编写
你可以试试这个…preg_replace():@Learning Do strlen是否被允许?:如果我们不使用它,它会很好。你想不使用strlen这样的内置函数来解决这个问题吗?它确实有效,但这个程序的目的是不使用内置函数。我知道我用过strlen。@Learning我更新了一个解决方案,可能不是最好的,但有效。。好的,我理解你,但是我不认为不使用strops或strstr就可以做到这一点,或者(内爆/爆炸)要从主字符串中删除部分字符串这是需要内置函数的主要部分
<?php
ini_set('display_errors', 1);
$string="Hello world";
echo strReplace($string,"llo","zz");
echo strReplace($string,"o","xx");
function strReplace($string,$toReplace,$replacement)
{
while($indexArray=checkSubStringIndexes($toReplace,$string))
{
$stringArray= getChars($string);
$replaced=false;
$newString="";
foreach($stringArray as $key => $value)
{
if(!$replaced && in_array($key,$indexArray))
{
$newString=$newString.$replacement;
$replaced=true;
}
elseif(!in_array($key,$indexArray))
{
$newString=$newString.$value;
}
}
$string=$newString;
}
return $string;
}
function getLength($string)
{
$counter=0;
while(true)
{
if(isset($string[$counter]))
{
$counter++;
}
else
{
break;
}
}
return $counter;
}
function getChars($string)
{
$result=array();
$counter=0;
while(true)
{
if(isset($string[$counter]))
{
$result[]=$string[$counter];
$counter++;
}
else
{
break;
}
}
return $result;
}
function checkSubStringIndexes($toReplace,$string)
{
$counter=0;
$indexArray=array();
$newCounter=0;
$length= getLength($string);
$toReplacelength= getLength($toReplace);
$mainCharacters= getChars($string);
$toReplaceCharacters= getChars($toReplace);
for($x=0;$x<$length;$x++)
{
if($mainCharacters[$x]==$toReplaceCharacters[0])
{
for($y=0;$y<$toReplacelength;$y++)
{
if(isset($mainCharacters[$x+$y]) && $mainCharacters[$x+$y]==$toReplaceCharacters[$y])
{
$indexArray[]=$x+$y;
$newCounter++;
}
}
if($newCounter==$toReplacelength)
{
return $indexArray;
}
}
}
}
<?php
function stringreplce($str,$strreplace,$strreplaceby){
$str_array=explode($strreplace,$str);
$newstr=implode($strreplaceby,$str_array);
return $newstr;
}
echo stringreplce("Hello World","llo","zz")."<br>";
echo stringreplce("Hello World","Hel","zz")."<br>";
echo stringreplce("Hello World"," Wo","zz")."<br>";
echo stringreplce("Hello World","rl","zz")."<br>";
echo stringreplce("Hello World","ld","zz")."<br>";
?>
function ganti_kata($kata,$kata_awal,$kata_ganti){
$i =0; $hasil;
$panjang = strlen($kata);
while ($i < $panjang) {
if ($kata[$i] == $kata_awal) {
$hasil[$i] = $kata_ganti;
echo $hasil[$i];
}
else if ($kata[$i] !== $kata_awal){
$hasil[$i] = $kata[$i];
echo $hasil[$i];
}
$i++;
}
}
echo ganti_kata('Riowaldy Indrawan','a','x');