PHP - 打印结果问题

时间:2017-05-11 19:05:39

标签: php

我从类中进行了这个练习,我们需要使用一个函数来创建脚本,该函数使用给定的字符串作为小写参数,我必须以大写形式返回相同的字符串,我已经做了,但是当它涉及到打印,它按字母顺序打印字母,我无法弄清楚什么是错的,这里是代码:

 <html>
 <head>
 <title>Ejercicio 10</title>
 </head>
 <body>

 <center><h1>Ejercicio 10 (Función 1)</h1></center>

 <h1>
 <center>

<form action="ejercicio10.php" method="post">
 <input type="text" id="frase" name="frase" placeholders="Introduzca una frase para transformar a mayusculas"/>
 <input type="submit" value="Enviar"/>
</form>

<?php

$frase = $_POST['frase'];

function mayusculas($frase) {

$longitud = strlen($frase);
$minusculas = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","w","x","y","z"," ");
$mayusculas = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","W","X","Y","Z"," ");
$numeroLetras = count($minusculas);

for ($i = 0; $i < $longitud + 1; $i++) {

 for ($j=0; $j < $numeroLetras ; $j++) {

  $resultado = strrpos($frase, $minusculas[$j], $i);

    if($resultado !== FALSE){

    print($mayusculas[$j]);
    }
   }

   return null;

  }

}

  print(mayusculas($frase));

 ?>

</center>
</h1>

</body>
</html>

3 个答案:

答案 0 :(得分:0)

使用此循环for ($j=0; $j < $numeroLetras ; $j++) {并执行此$resultado = strrpos($frase, $minusculas[$j], $i);意味着您基本上只在$minusculas数组上循环,这是按字母顺序排列的。

您的代码中的另一个错误是,如果您的字符串包含重复字母hkgkh,则它只会打印GKH。

您的代码中存在的主要问题是return null循环中的for ($i = 0; $i < $longitud + 1; $i++) {语句。这只会让你的循环变得有趣,只需1次迭代。

答案 1 :(得分:0)

好的,为了练习,这是正在发生的事情。

您正在使用两个循环。外循环迭代的次数与字符串的长度一样多,内循环遍历每个字母A到Z.

这是正确的工作版本,然后我将解释:

function mayusculas($frase) {

    $longitud = strlen($frase);
    $minusculas = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","w","x","y","z"," ");
    $mayusculas = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","W","X","Y","Z"," ");
    $numeroLetras = count($minusculas);

    for ($i = 0; $i < $longitud + 1; $i++) {

        for ($j=0; $j < $numeroLetras ; $j++) {

            $resultado = strpos($frase, $minusculas[$j], $i);

            if($resultado == $i && $resultado !== false) {
                print($mayusculas[$j]);
                break;
            }
        }

    }

}

你的for循环是正确的,它是你在里面做的不是。以下需要发生:

  • 首先,你应该使用strpos而不是strrpos。我们正在翻阅前进一词中的字母,而不是倒退。

  • 当我们检查结果中的false时,我们还需要检查它找到的字母位置是我们要查找的字母的位置(即来自外循环)

  • 一旦我们在正确的位置找到匹配的字母,您就可以输出它。然后我们想要停止内循环迭代,因为我们已完成该循环。

  • 最后,在外部循环的第一次迭代之后,您返回null,这是不正确的。这会阻止外循环移动到第二个字母。我们需要让循环运行。

最后一点,这不是接近最佳方法的地方。我只是为了学习而突出显示。

答案 2 :(得分:0)

如果不必要,使用函数import { combineReducers } from 'redux' import { reducer as formReducer } from 'redux-form ...... const reducerList = { GLVersion, ServerConfig, ServerStats, form: formReducer } export default combineReducers(reducerList) 的另一个选项是:

在你的PHP部分:

    <Panel className='configPanel'
      collapsible header="Configuration"
      eventKey="1"
      defaultExpanded={true}>
      <Configuration onSubmit={(config) => writeConfig(config)} />
    </Panel>