Hướng dẫn php trim not working - php trim không hoạt động

Tôi đang cố gắng sử dụng Trim để loại bỏ ký tự gạch chân khỏi dữ liệu được trả về trong mảng $ _POST. Tôi đã thử sử dụng

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

Nhưng văn bản dường như không trả lại như một chuỗi duy nhất. Nó bị hỏng giữa mỗi mục. Sau đó, tôi đã thử trang trí như thế này:

  $value)
    {
    $str = $str . $key . ",";
    }

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

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

Khi tôi sử dụng hàm cắt, không có gì xảy ra, nó không xóa ký tự _. Mục tiêu cuối cùng của tôi là đưa một danh sách dấu vết dấu phẩy trong cơ sở dữ liệu của tôi dưới dạng một chuỗi duy nhất. Tại sao chức năng trim() của tôi không hoạt động trong trường hợp này?

CẬP NHẬT: Tìm thấy
Trong xem tài nguyên trang, vì vậy tôi phải thực hiện kết hợp các mục sau:
Found
in view page resource so I had to do a combination of the following:

       $post_Value= str_replace("", "", $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";

Không có người dùng đóng góp ghi chú cho trang này.

(Php 4, Php 5, Php 7, Php 8)Strip whitespace (or other characters) from the beginning and end of a string

Cắt - dải trắng (hoặc các ký tự khác) từ đầu và cuối chuỗi

Sự mô tả(string $string, string $characters = " \n\r\t\v\x00"): string

  • Trim (Chuỗi $string, Chuỗi $characters = "\ n \ r \ t \ v \ x00"): Chuỗi
  • "" (ASCII
      $value)
        {
        $str = $str . $key . ",";
        }
    
       $post_Value = trim("_", $str);
       }
    
       $query = "UPDATE player_match SET categoryOption='$$post_Value' WHERE id=1";
     ?>
    
    0 (
      $value)
        {
        $str = $str . $key . ",";
        }
    
       $post_Value = trim("_", $str);
       }
    
       $query = "UPDATE player_match SET categoryOption='$$post_Value' WHERE id=1";
     ?>
    
    1)), một không gian thông thường.
  • "\ t" (ASCII
      $value)
        {
        $str = $str . $key . ",";
        }
    
       $post_Value = trim("_", $str);
       }
    
       $query = "UPDATE player_match SET categoryOption='$$post_Value' WHERE id=1";
     ?>
    
    2 (
      $value)
        {
        $str = $str . $key . ",";
        }
    
       $post_Value = trim("_", $str);
       }
    
       $query = "UPDATE player_match SET categoryOption='$$post_Value' WHERE id=1";
     ?>
    
    3)), một tab.
  • "\ n" (ASCII
      $value)
        {
        $str = $str . $key . ",";
        }
    
       $post_Value = trim("_", $str);
       }
    
       $query = "UPDATE player_match SET categoryOption='$$post_Value' WHERE id=1";
     ?>
    
    4 (
      $value)
        {
        $str = $str . $key . ",";
        }
    
       $post_Value = trim("_", $str);
       }
    
       $query = "UPDATE player_match SET categoryOption='$$post_Value' WHERE id=1";
     ?>
    
    5)), một dòng mới (nguồn cấp dữ liệu).
  • "\ r" (ASCII
      $value)
        {
        $str = $str . $key . ",";
        }
    
       $post_Value = trim("_", $str);
       }
    
       $query = "UPDATE player_match SET categoryOption='$$post_Value' WHERE id=1";
     ?>
    
    6 (
      $value)
        {
        $str = $str . $key . ",";
        }
    
       $post_Value = trim("_", $str);
       }
    
       $query = "UPDATE player_match SET categoryOption='$$post_Value' WHERE id=1";
     ?>
    
    7)), trả lại vận chuyển.
  • "\ 0" (ASCII
      $value)
        {
        $str = $str . $key . ",";
        }
    
       $post_Value = trim("_", $str);
       }
    
       $query = "UPDATE player_match SET categoryOption='$$post_Value' WHERE id=1";
     ?>
    
    8 (
      $value)
        {
        $str = $str . $key . ",";
        }
    
       $post_Value = trim("_", $str);
       }
    
       $query = "UPDATE player_match SET categoryOption='$$post_Value' WHERE id=1";
     ?>
    
    9)), ________ 20-byte.

Thông số

       $post_Value= str_replace("", "", $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";
3

Chuỗi sẽ được cắt tỉa.string that will be trimmed.

       $post_Value= str_replace("", "", $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";
4

Tùy chọn, các ký tự bị tước cũng có thể được chỉ định bằng tham số

       $post_Value= str_replace("", "", $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";
4. Chỉ cần liệt kê tất cả các ký tự mà bạn muốn bị tước. Với
       $post_Value= str_replace("", "", $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";
6, bạn có thể chỉ định một loạt các ký tự.

Trả về giá trị

Chuỗi được cắt.

Ví dụ

Ví dụ #1 Ví dụ sử dụng của Trim ()trim()

       $post_Value= str_replace("", "", $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";
7

       $post_Value= str_replace("", "", $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";
8

       $post_Value= str_replace("", "", $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";
9

Ví dụ trên sẽ xuất ra:

string(32) "        These are a few words :) ...  "
string(16) "    Example string
"
string(11) "Hello World"

string(28) "These are a few words :) ..."
string(24) "These are a few words :)"
string(5) "o Wor"
string(9) "ello Worl"
string(14) "Example string"

Ví dụ #2 Các giá trị mảng cắt bằng trang trí ()trim()

string(32) "        These are a few words :) ...  "
string(16) "    Example string
"
string(11) "Hello World"

string(28) "These are a few words :) ..."
string(24) "These are a few words :)"
string(5) "o Wor"
string(9) "ello Worl"
string(14) "Example string"
0

Ví dụ trên sẽ xuất ra:

array(3) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(7) "banana "
  [2]=>
  string(11) " cranberry "
}
array(3) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(6) "banana"
  [2]=>
  string(9) "cranberry"
}

Ví dụ #2 Các giá trị mảng cắt bằng trang trí ()

Ghi chú: Possible gotcha: removing middle characters

Lưu ý: Có thể Gotcha: Xóa các ký tự giữatrim() trims characters from the beginning and end of a string, it may be confusing when characters are (or are not) removed from the middle.

string(32) "        These are a few words :) ...  "
string(16) "    Example string
"
string(11) "Hello World"

string(28) "These are a few words :) ..."
string(24) "These are a few words :)"
string(5) "o Wor"
string(9) "ello Worl"
string(14) "Example string"
1 removes both 'a' and 'b' because it trims 'a' thus moving 'b' to the beginning to also be trimmed. So, this is why it "works" whereas
string(32) "        These are a few words :) ...  "
string(16) "    Example string
"
string(11) "Hello World"

string(28) "These are a few words :) ..."
string(24) "These are a few words :)"
string(5) "o Wor"
string(9) "ello Worl"
string(14) "Example string"
2 seemingly does not.

Bởi vì Trim () cắt các ký tự từ đầu và cuối chuỗi, nó có thể gây nhầm lẫn khi các ký tự (hoặc không) bị xóa khỏi giữa. string(32) " These are a few words :) ... " string(16) " Example string " string(11) "Hello World" string(28) "These are a few words :) ..." string(24) "These are a few words :)" string(5) "o Wor" string(9) "ello Worl" string(14) "Example string" 1 loại bỏ cả 'A' và 'B' vì nó cắt 'do' di chuyển 'B' đến đầu cũng được cắt tỉa. Vì vậy, đây là lý do tại sao nó "hoạt động" trong khi string(32) " These are a few words :) ... " string(16) " Example string " string(11) "Hello World" string(28) "These are a few words :) ..." string(24) "These are a few words :)" string(5) "o Wor" string(9) "ello Worl" string(14) "Example string" 2 dường như không.

  • Xem thêm
  • LTRIM () - Dải khoảng trắng (hoặc các ký tự khác) từ đầu chuỗi
  • rtrim () - dải trắng (hoặc các ký tự khác) từ cuối chuỗi

str_replace () - Thay thế tất cả các lần xuất hiện của chuỗi tìm kiếm bằng chuỗi thay thế