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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题