2015-02-17 45 views
2

我試圖使用修剪從$ _POST數組中返回的數據中刪除下劃線字符。我試過使用爲什麼我的php trim()函數不工作?

$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()功能在這種情況下不起作用?

UPDATE:發現<br/>鑑於頁面資源,所以我不得不做以下的組合:

 $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"; 
+0

如果要刪除下劃線,你可以用'str_replace()函數',但第二放慢參數應該是一個空字符串,所以它應該看起來像:'str_replace(「_」,「」,$ key)'。 – marian0 2015-02-17 23:02:15

回答

4

首先,trim()發生在相反的順序參數:$str,然後$character_mask。所以,你應該用:$post_Value = trim($str, "_");

其次,trim()串蒙面字符只有從一開始就和結束串的。如果它們被非屏蔽字符包圍,它不會從字符串中刪除任何屏蔽字符。


你應該實際使用str_replace()一個空字符串替換(你試過一個空間置換):

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

如果您也想刪除其典型<br>標籤(變化),您可以通過單str_replace()調用做到這一點,如下所示:

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

str_replace()文檔的詳細信息。

+0

我切換了參數,並將每個條目放在新行上而不是單個字符串。 – CloudyKooper 2015-02-17 23:10:27

+0

我在查看頁面資源時發現了一些
。我的代碼中沒有他們,所以這很混亂。我終於必須像這樣做str_replace()和rtrim()的組合:$ post_Value = str_replace(「」,「」,$ str); \t \t \t $ post_Value2 = str_replace(「_」,「」,$ post_Value); \t \t \t $ post_Value3 = rtrim($ post_Value2,「,submit,」); \t \t \t echo $ post_Value3; \t \t \t // echo $ editedStr = str_replace(「_」,「」,$ str); \t \t \t \t \t \t $查詢= 「UPDATE player_match SET categoryOption = '$ post_Value3' WHERE ID = 1」; – CloudyKooper 2015-02-17 23:50:54

0

我在查看頁面資源時發現了一些
。我的代碼中沒有他們,所以這很混亂。最後我不得不做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"; 
相關問題