【问题标题】:string replace without using any in-built function字符串替换而不使用任何内置函数
【发布时间】:2017-05-14 06:31:33
【问题描述】:

用一个程序在不使用str_replace的情况下替换字符串中的子字符串

应该是通用函数应该适用于以下示例:

  1. 字:你好,世界 替换词:llo 替换为:zz 输出应该是:Hezz world

  2. 字:你好,世界 替换单词:o 替换为:xx 输出应该是:Hellxx wxxrld

这是我为解决它而写的

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;
}

我得到了 helzzworld 的输出。 请看看我做错了什么,或者如果您对此有更好的解决方案,请提出建议。

【问题讨论】:

  • 你可以试试这个... preg_replace():
  • @Learning strlen 是否允许? :D
  • 如果我们不使用它会很好。
  • 你想在不使用 strlen 等内置函数的情况下解决这个问题吗?

标签: php arrays string algorithm


【解决方案1】:

您可以通过将主字符串分解为数组然后将您的 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>";
    ?>

【讨论】:

  • 好的,我理解你,但我不认为你可以不使用 strops 或 strstr 或者(内爆/爆炸)你想从你的主字符串中删除部分字符串这是主要的需要内置功能的部分
【解决方案2】:

试试这个最简单的,不使用str_replace,这里我们使用explodeimplodesubstr_count

1. substr_count 用于计数和检查子字符串的存在。

2. explode 用于根据匹配的子字符串将字符串分解为数组。

3. implode 用替换加入字符串。

Try this code snippet here

<?php
ini_set('display_errors', 1);
$string="Hello world";
echo strReplace($string,"llo","zz");
echo strReplace($string,"o","xx");
function strReplace($string,$toReplace,$replacement)
{
    if(substr_count($string, $toReplace))
    {
        $array=explode($toReplace,$string);
        return implode($replacement, $array);
    }
}

解决方案 2:

Try this code snippet herenot best, but working

<?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;
            }
        }
    }
}

【讨论】:

  • 确实有效,但该程序的目的是不使用内置函数。我知道我确实使用过 strlen。
  • @Learning 我已经更新了一个解决方案,可能不是最好的但可以工作..
【解决方案3】:
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');

这段代码使用php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 2021-09-08
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2019-04-18
    相关资源
    最近更新 更多