php 在不使用任何字符串函数的情况下反转php中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34415889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:58:07  来源:igfitidea点击:

Reverse a string in php without using any string function

php

提问by Shakun Chaudhary

In yesterday Interview I was asked that how to reverse a string with out using any string function like strrev() or strlen(). I found this example on website but it gives error.is it possible to do this without strlen().

在昨天的采访中,我被问到如何在不使用任何字符串函数(如 strrev() 或 strlen())的情况下反转字符串。我在网站上找到了这个例子,但它给出了错误。是否可以在没有 strlen() 的情况下做到这一点。

Uninitialized string offset: -1 in D:\xampp\htdocs\PhpProject1\index.php on line xx

未初始化的字符串偏移量:-1 in D:\xampp\htdocs\PhpProject1\index.php on line xx

$string = "ZEESHAN";

$i =0;

while(($string[$i])!=null){

        $i++;

}

$i--;

while(($string[$i])!=null){

        echo $string[$i];

$i--;

}

回答by Surace

$string = 'zeeshan';
$reverse = '';
$i = 0;
while(!empty($string[$i])){ 
      $reverse = $string[$i].$reverse; 
      $i++;
}
echo $reverse;

回答by Ashu Jha

I have created a simple logic for string reversal

我为字符串反转创建了一个简单的逻辑

$str = 'ABCD';
$strReversed = '';
$length = strlen($str);
for($i=$length-1; $i >= 0; $i--){
    $strReversed .= $str[$i];
}
echo $strReversed;

回答by Sougata Bose

Try -

尝试 -

$string = "ZEESHAN";

$j = 0;
while(!empty($string[$j])) {
   $j++;
}

for($i = ($j-1); $i >= 0; $i--) {
    echo $string[$i];
}

Output

输出

NAHSEEZ

回答by Miguel

You can do this

你可以这样做

$string = "heya";
$i = strlen("heya") - 1;
$newStr = "";

while($i >= 0) {
    $newStr .= $string[$i];
    $i--;
}
echo $newStr; // output: ayeh

回答by Miguel

Please take a look the below code: Reverse the string

请看下面的代码:反转字符串

$str = 'abcdefg';
    $reverseString = '';
    for($i=strlen($str);$i<=strlen($str);$i--){
      $reverseString .= $str[$i];
    if($i==0)
    break;
    }

    echo $reverseString;

The above code output is:

上面的代码输出是:

gfedcba

回答by ABHISHEK GUPTA

//Program for reversing a string
<?php
class str_rev{
    public function Revese_Str($string){
        $reverse = '';
        $i=0;
        while(!empty($string[$i])){ 
          $reverse = $string[$i].$reverse; 
          $i++;
        }
        return $reverse;
    }
}
$object = new str_rev();
echo $object->Revese_Str('Ayush Jain');

回答by Hemant Kumar

There is another method to do this i.e. "Recursion"

还有另一种方法可以做到这一点,即“递归”

<?php
/*
 String reverse without using functions and loop conditions
*/
$str = 'Hello World!';
echo rev($str);
function rev($str)
{  
  $len = strlen($str);
  /* Base case for recursion */
  if($len == 1 ){
    return $str;      
  }else{
    $len--; 
    /* extract last character and concatenate at end of string
      returned from recursive call on remaining string               
    */        
    return substr($str,$len) . rev(substr($str,0,$len));           

  }
}

回答by Rakesh

<?php
$string = 'ZEESHAN';
$reverse = '';
$i = 0;
while(!empty($string[$i])){
    $reverse = $string[$i].$reverse;
    $i++;
}
echo $reverse;
?>

Check complete post: https://www.troposal.com/reverse-string-without-function-php/

检查完整的帖子:https: //www.troposal.com/reverse-string-without-function-php/

回答by Kheshav Sewnundun

You can try it with count_charsand array_sum

你可以试试count_charsarray_sum

<?php

$string = "ZEESHAN";
$count=array_sum(count_chars($string));

for($i=$count -1 ;$i>=0;$i--){
        echo $string[$i];
}
?>

If you dont want any php string function you can try this way with krsortand array:

如果您不想要任何 php 字符串函数,您可以使用krsortand尝试这种方式array

<?php

$string = "ZEESHAN";
$arr = array();
$i=0;
while(isset($string[$i])){
        $arr[$i] = $string[$i];
        $i++;
}
krsort($arr);
$final = implode("",$arr);

var_dump($final);
?>

回答by Narendrasingh Sisodia

You can use whileand forloop like as

您可以像这样使用whilefor循环

$string = "ZEESHAN";
$reverse = "";
$j = 0;
while(isset($string[$j])){
    $j++;
}

for($i = $j - 1; $i >= 0; $i--){
    $reverse .= $string[$i];
}
echo $reverse;