dongliyu3278 2013-02-06 01:17
浏览 28
已采纳

Strtoupper()PHP用于混合字符串

I've been working on a project for school where I have you search for a string, and it wil query a database and return all of the words which are in that string of letters. For that, I have to use strtoupper(), which works fine if you have a string of all uppercase or lowercase letters. If you enter AAB or aab into the search, everything will work fine, and it will return the two anagrams, ABA and BAA. However, if you type in aAB, it will return nothing. So it is getting the post data from the input, named alpha, and then it is alphabetizing the word, so if you typed in ABA, it would return AAB, and then making it uppercase.

<title>Scrabble</title>
<?php
require 'connect.inc.php';
if (isset($_POST['al'])){
    $al=$_POST['al'];
    $al=alpha($al);
    $al=trim(strtoupper($al));
    $query="SELECT * from Words WHERE alpha='$al'";
    if ($query_run = mysql_query($query)){

        while ($query_row = mysql_fetch_assoc($query_run)){
            $alpha = $query_row['alpha'];
            $ana = $query_row['word'];

            echo "<strong>$ana</strong> $alpha<br>";

        }
    }


}
function alpha($word){
    $array=array();
    for($x=0;$x<strlen($word);$x++){
        $char=substr($word,$x,1);
        $array[$x]=$char;


    }
    sort($array);
    $alpha=implode('',$array);
    return $alpha;

}
?>
<form action='scrabble.php' method='POST'>
Enter text to anagram. Please use either all uppercase or all lowercase<input type='text'                   name='al'>
<input type='submit'>
</form>

he link is here http://newdev.shodor.org/~amalani/newdev/scrabble.php Thanks

  • 写回答

1条回答 默认 最新

  • dongnuoyi8833 2013-02-06 01:40
    关注

    Multiple issues:

    PHP's sort function returns a boolean for whether it was successful or not. In your code you have:

    $array=sort($array);
    

    When it should just be:

    sort($array);
    

    You have a syntax error near the top:

    $alphagram=trim(strtoupper($al);
    

    Should probably be:

    $alphagram = trim(strtoupper($alphagram));
    

    I also noticed that on your website, you use:

    name='al'
    

    And then in the script try to access the POST variable alpha. You should be using:

    $_POST['al']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)