【问题标题】:Why is my php trim() function not working?为什么我的 php trim() 函数不起作用?
【发布时间】:2015-02-17 22:50:01
【问题描述】:

我正在尝试使用 trim 从 $_POST 数组中返回的数据中删除下划线字符。我尝试使用

 $post_Value= str_replace("_", " ", $key) 

但文本似乎没有作为单个字符串返回。它在每个条目之间被打破。然后我尝试像这样修剪:

 <?php
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);

 // Test if connection succeeded

 if (mysqli_connect_errno())
    {
    die("Database connection failed: " . mysqli_connect_error() . " (" .  mysqli_connect_errno() . ")");
}

 if (isset($_POST))
    {
    $str = "";
   foreach($_POST as $key => $value)
    {
    $str = $str . $key . ",";
    }

   $post_Value = trim("_", $str);
   }

   $query = "UPDATE player_match SET categoryOption='$$post_Value' WHERE id=1";
 ?>

当我使用修剪功能时,没有任何反应,它不会删除_ 字符。我的最终目标是将逗号分隔的列表作为单个字符串放入我的数据库中。为什么我的trim() 函数在这种情况下不起作用?

更新:在视图页面资源中找到&lt;br/&gt;,因此我必须结合以下操作:

       $post_Value= str_replace("<br_/>", "", $str);
        $post_Value2= str_replace("_", " ", $post_Value);
        $post_Value3= rtrim($post_Value2,",submit,");
        echo $post_Value3;


        $query="UPDATE player_match SET categoryOption='$post_Value3' WHERE   id=1";

【问题讨论】:

  • 如果你想去掉下划线,你可以使用str_replace(),但是第二个参数应该是一个空字符串,所以它应该是:str_replace("_", "", $key)

标签: php trim


【解决方案1】:

首先,trim() 采用 相反顺序的参数$str,然后是 $character_mask。所以你应该使用:$post_Value = trim($str, "_");

其次,trim() 将屏蔽字符仅从字符串的开头和结尾字符串化。如果字符串被非屏蔽字符包围,它不会从字符串中删除任何屏蔽字符。


你实际上应该使用 str_replace() 和一个空的替换字符串(你已经尝试了一个空格作为替换):

$post_Value= str_replace("_", "", $key)

如果您还想删除 &lt;br&gt; 标记(在其典型变体中),您可以通过单个 str_replace() 调用来执行此操作,如下所示:

$post_Value= str_replace(array("_", "<br>", "<br/>", "<br />"), "", $key)

有关详细信息,请参阅str_replace() 文档。

【讨论】:

  • 我切换了参数并将每个条目放在一个新行上,而不是一个字符串。
  • 我在查看页面资源时发现了一些
    。我的代码中没有它们,所以这很混乱。我终于不得不像这样组合 str_replace() 和 rtrim() : $post_Value= str_replace("", "", $str); $post_Value2=str_replace("", "", $post_Value); $post_Value3= rtrim($post_Value2,",提交,");回声 $post_Value3; //echo $editedStr=str_replace("", " ", $str); $query="UPDATE player_match SET categoryOption='$post_Value3' WHERE id=1";
【解决方案2】:

我在查看页面资源时发现了一些
。我的代码中没有它们,所以这很混乱。我终于不得不像这样将 str_replace() 和 rtrim() 组合起来:

 $post_Value= str_replace("<br_/>", "", $str); $post_Value2= str_replace("", " ", $post_Value); $post_Value3= rtrim($post_Value2,",submit,"); echo $post_Value3; //echo $editedStr=str_replace("", " ", $str); $query="UPDATE player_match SET categoryOption='$post_Value3' WHERE id=1";

【讨论】:

    【解决方案3】:

    试试

    preg_replace( '/^\W*(.*?)\W*$/', '$1', $string )
    
    /* -----------------------------------------------------------------------------------
      ^                        the beginning of the string
        \W*                    non-word characters (all but a-z, A-Z, 0- 9, _) (0 or more times (matching the most amount possible))
        (                      group and capture to \1:
          .*?                  any character except \n (0 or more times(matching the least amount possible))
        )                      end of \1
        \W*                    non-word characters (all but a-z, A-Z, 0-9, _) (0 or more times (matching the most amount possible))
      $                        before an optional \n, and the end of the string
    ------------------------------------------------------------------------------------- */
    

    【讨论】:

      猜你喜欢
      • 2023-02-09
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      • 2021-12-18
      • 2011-06-23
      • 1970-01-01
      • 2017-08-18
      相关资源
      最近更新 更多