简体   繁体   English

PHP添加前导空间,尝试修剪,ltrim,str_replace

[英]PHP adding leading space, tried trim, ltrim, str_replace

I'm trying to take input, gameName, which is very sensitive to leading spaces as it needs to work with other functions. 我正在尝试输入gameName,它对前导空格非常敏感,因为它需要与其他功能一起使用。

When I enter it into the input field, there is no space. 当我将其输入到输入字段中时,没有空格。 When I check it in the DB, directly after being processed, there is a space in front of it. 当我在数据库中检查它时,在处理完之后,它前面立即有一个空格。 When I pull it from the DB, there is no space, and reinserting it to the DB (after being pulled) removes the space. 当我从数据库中拉出它时,没有空间,将其重新插入到数据库中(在被拉出之后)会删除该空间。

The offending code for the page is: 该页面的有害代码为:

if (isset($_GET) && $_GET['page'] == 'addGame') {
    if (isset($_GET) && $_GET['page'] == 'addGame' && $_GET['action'] == 'success') {
?>
      <h2>Game added to database.</h2>
      <h4><a href='index.php?page=addGame'>Add another</a> or choose something new to do.</h4>
<?
    } else {
      include('./inc/addGameProc.php');
?>
      <fieldset>
      <legend><h2>Add game</h2></legend>

      <form name='addGame' id='addGame' method='POST'>
        <input type='text' name='gameName' placeholder='Game name' required /><br />
        <textarea name="gameDesc" class="span12" rows="10" placeholder='Game description' required></textarea><br />
        <input type='submit' name='submitNewGame' class='btn btn-primary' /><br />
      </form>
      </fieldset>
<?

    }
}

The add game proc you see is: 您看到的添加游戏过程为:

    <?
    if (isset($_POST['submitNewGame']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
        $database = new database('localhost','wwwander_sradmin','editedout','wwwander_srmain','mysqli');
        $database->openConnection();

            //save and escape entered material
            $gameName = ltrim($_POST['gameName']);
            $gameDesc = $database->escapeString($_POST['gameDesc']);

            //check that material not already in db
            $checkDB = $database->queryDB("SELECT * FROM mainSite_games WHERE gameName='$gameName' AND gameDesc='$gameDesc'");
            if ($database->dbNumRows($checkDB) == 1) {
                echo "Game already in database";
            } else {
                //add entered material into db
                $addToDB = $database->queryDB("INSERT INTO mainSite_games(gameName, gameDesc) VALUES(\" $gameName \", \" $gameDesc \")");
                if (!$addToDB) {
                    echo "An error occured.";
                } else {
                    header('Location: index.php?page=addGame&action=success');
                }
            }

        $database->closeConnection();
    }
?>

You can see my tried and failed ltrim there. 您可以在那里看到我尝试过并失败的ltrim。

Of course, SO is my last try here. 当然,这是我最后一次尝试。 I've done all I know. 我已经尽力了 Any ideas? 有任何想法吗? Any more resources I can provide that I've left out? 我还有什么资源可以提供的?

EDIT-- 编辑 -

After being pulled, there IS a space. 拉动后,有一个空间。 The reason I said there wasn't is because where I usually pull it, it's going into a select, where I suppose spaces don't matter since they aren't taken into account? 我之所以说没有,是因为我通常将其拉到一个选择中,在这里我认为空格无关紧要,因为未考虑空格? I dunno. 我不知道。

Change : 变更:

VALUES(\" $gameName \" 

to : 至 :

VALUES(\"$gameName\"

I think the space you're seeing is the one directly after the quotes 我认为您所看到的空间是引号后面的空格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM