Làm cách nào để cắt 2 ký tự đầu tiên trong php?

Thỉnh thoảng, chúng ta phải xử lý các chuỗi chứa các ký tự không mong muốn ở đầu hoặc cuối của chúng. PHP cung cấp rất nhiều phương thức có thể được sử dụng để loại bỏ các ký tự này. mỗi người trong số họ đều có những ưu điểm và nhược điểm riêng. Trong hướng dẫn này, chúng tôi sẽ thảo luận chi tiết về tất cả các phương pháp này để bạn có thể chọn phương pháp phù hợp nhất với tình huống của mình

Trên trang này

Xóa các ký tự 'n' cuối cùng khỏi chuỗi bằng cách sử dụng substr[]

Hàm PHP substr[] trả về một phần của chuỗi được chỉ định bởi các tham số

$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_first_one   = substr[$sentence, 1];
$removed_first_two   = substr[$sentence, 2];
$removed_first_three = substr[$sentence, 3];
$removed_first_four  = substr[$sentence, 4];
$removed_first_twenty  = substr[$sentence, 20];

// Output — "n apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_one;

// Output — " apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_two;

// Output — "apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_three;

// Output — "pple a day keeps anyone away if you throw it hard enough."
echo $removed_first_four;

// Output — " anyone away if you throw it hard enough."
echo $removed_first_twenty;
9 và
$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
0. Nếu bạn muốn xóa các ký tự ở cuối chuỗi, bạn có thể đặt giá trị của
$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_first_one   = substr[$sentence, 1];
$removed_first_two   = substr[$sentence, 2];
$removed_first_three = substr[$sentence, 3];
$removed_first_four  = substr[$sentence, 4];
$removed_first_twenty  = substr[$sentence, 20];

// Output — "n apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_one;

// Output — " apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_two;

// Output — "apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_three;

// Output — "pple a day keeps anyone away if you throw it hard enough."
echo $removed_first_four;

// Output — " anyone away if you throw it hard enough."
echo $removed_first_twenty;
9 thành 0 và giá trị của
$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
0 thành số âm

Bạn có thể đặt giá trị của

$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
0 thành -1 để xóa ký tự cuối cùng của chuỗi. Tương tự, bạn có thể đặt
$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
0 thành -2 để xóa hai ký tự cuối khỏi chuỗi. Nói một cách đơn giản, hãy đặt giá trị của thuộc tính
$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
0 thành số âm của số ký tự mà bạn muốn xóa khỏi cuối chuỗi

Trong ví dụ sau, chúng tôi đã sử dụng hàm

$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_first_one   = substr[$sentence, 1];
$removed_first_two   = substr[$sentence, 2];
$removed_first_three = substr[$sentence, 3];
$removed_first_four  = substr[$sentence, 4];
$removed_first_twenty  = substr[$sentence, 20];

// Output — "n apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_one;

// Output — " apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_two;

// Output — "apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_three;

// Output — "pple a day keeps anyone away if you throw it hard enough."
echo $removed_first_four;

// Output — " anyone away if you throw it hard enough."
echo $removed_first_twenty;
4 để xóa số lượng ký tự khác nhau khỏi cuối chuỗi

PHP

$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_last_one   = substr[$sentence, 0, -1];
$removed_last_two   = substr[$sentence, 0, -2];
$removed_last_three = substr[$sentence, 0, -3];
$removed_last_four  = substr[$sentence, 0, -4];
$removed_last_twenty  = substr[$sentence, 0, -20];

// Output — An apple a day keeps anyone away if you throw it hard enough
echo $removed_last_one;

// Output — An apple a day keeps anyone away if you throw it hard enoug
echo $removed_last_two;

// Output — An apple a day keeps anyone away if you throw it hard enou
echo $removed_last_three;

// Output — An apple a day keeps anyone away if you throw it hard eno
echo $removed_last_four;

// Output — An apple a day keeps anyone away if you t
echo $removed_last_twenty;

Xóa các ký tự 'n' đầu tiên khỏi chuỗi bằng substr[]

Bạn cũng có thể sử dụng hàm substr[] để xóa các ký tự ở đầu chuỗi bằng cách chỉ định giá trị khác 0 cho tham số

$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
0. Tham số
$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
0 là tùy chọn trong chức năng này. Bỏ qua nó sẽ trả về toàn bộ chuỗi bắt đầu từ chỉ mục
$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_first_one   = substr[$sentence, 1];
$removed_first_two   = substr[$sentence, 2];
$removed_first_three = substr[$sentence, 3];
$removed_first_four  = substr[$sentence, 4];
$removed_first_twenty  = substr[$sentence, 20];

// Output — "n apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_one;

// Output — " apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_two;

// Output — "apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_three;

// Output — "pple a day keeps anyone away if you throw it hard enough."
echo $removed_first_four;

// Output — " anyone away if you throw it hard enough."
echo $removed_first_twenty;
9

Để xóa ký tự đầu tiên khỏi đầu chuỗi, hãy đặt giá trị của

$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_first_one   = substr[$sentence, 1];
$removed_first_two   = substr[$sentence, 2];
$removed_first_three = substr[$sentence, 3];
$removed_first_four  = substr[$sentence, 4];
$removed_first_twenty  = substr[$sentence, 20];

// Output — "n apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_one;

// Output — " apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_two;

// Output — "apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_three;

// Output — "pple a day keeps anyone away if you throw it hard enough."
echo $removed_first_four;

// Output — " anyone away if you throw it hard enough."
echo $removed_first_twenty;
9 thành 1. Để loại bỏ hai ký tự đầu tiên từ đầu, hãy đặt giá trị của
$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_first_one   = substr[$sentence, 1];
$removed_first_two   = substr[$sentence, 2];
$removed_first_three = substr[$sentence, 3];
$removed_first_four  = substr[$sentence, 4];
$removed_first_twenty  = substr[$sentence, 20];

// Output — "n apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_one;

// Output — " apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_two;

// Output — "apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_three;

// Output — "pple a day keeps anyone away if you throw it hard enough."
echo $removed_first_four;

// Output — " anyone away if you throw it hard enough."
echo $removed_first_twenty;
9 thành 2. Bạn có thể xóa ký tự 'n' đầu tiên khỏi chuỗi bằng cách đặt giá trị của
$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_first_one   = substr[$sentence, 1];
$removed_first_two   = substr[$sentence, 2];
$removed_first_three = substr[$sentence, 3];
$removed_first_four  = substr[$sentence, 4];
$removed_first_twenty  = substr[$sentence, 20];

// Output — "n apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_one;

// Output — " apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_two;

// Output — "apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_three;

// Output — "pple a day keeps anyone away if you throw it hard enough."
echo $removed_first_four;

// Output — " anyone away if you throw it hard enough."
echo $removed_first_twenty;
9 thành 'n' theo cách tương tự

PHP

$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_first_one   = substr[$sentence, 1];
$removed_first_two   = substr[$sentence, 2];
$removed_first_three = substr[$sentence, 3];
$removed_first_four  = substr[$sentence, 4];
$removed_first_twenty  = substr[$sentence, 20];

// Output — "n apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_one;

// Output — " apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_two;

// Output — "apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_three;

// Output — "pple a day keeps anyone away if you throw it hard enough."
echo $removed_first_four;

// Output — " anyone away if you throw it hard enough."
echo $removed_first_twenty;

Xóa các ký tự cụ thể khỏi cuối chuỗi bằng cách sử dụng
$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
1

Hàm rtrim[] trong PHP loại bỏ tất cả các ký tự ở cuối chuỗi đã cho được chỉ định trong

$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
2. Nếu bạn không đặt giá trị cho
$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
2, hàm này sẽ xóa NULL, tab, dòng mới, tab dọc, xuống dòng và khoảng trắng thông thường

Điều quan trọng cần nhớ là

$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
1 tiếp tục xóa các ký tự ở cuối chừng nào chúng còn tồn tại trong
$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
2. Các ví dụ sau đây sẽ làm cho nó rõ ràng

PHP

$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];

Trong trường hợp đầu tiên, chúng tôi chưa chỉ định giá trị cho

$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
2. Do đó, hàm
$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
1 chỉ xóa khoảng trắng ở cuối

Trong trường hợp thứ hai,

$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
2 chứa dấu cách [] và dấu chấm [
$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
9]. Do đó, hàm
$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
1 tiếp tục xóa các ký tự cuối cùng miễn là nó là dấu chấm hoặc khoảng trắng

Trong trường hợp thứ ba, hàm

$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
1 dừng ở dấu chấm vì nó không được bao gồm trong mặt nạ ký tự

Trong trường hợp cuối cùng, hàm

$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
1 tiếp tục xóa tất cả các khoảng trắng, dấu chấm, dấu phẩy, dấu chấm than và dấu chấm hỏi cho đến khi nó đạt đến một ký tự chữ cái

Xóa các ký tự cụ thể khỏi đầu chuỗi bằng cách sử dụng
$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_last_one   = substr[$sentence, 0, -1];
$removed_last_two   = substr[$sentence, 0, -2];
$removed_last_three = substr[$sentence, 0, -3];
$removed_last_four  = substr[$sentence, 0, -4];
$removed_last_twenty  = substr[$sentence, 0, -20];

// Output — An apple a day keeps anyone away if you throw it hard enough
echo $removed_last_one;

// Output — An apple a day keeps anyone away if you throw it hard enoug
echo $removed_last_two;

// Output — An apple a day keeps anyone away if you throw it hard enou
echo $removed_last_three;

// Output — An apple a day keeps anyone away if you throw it hard eno
echo $removed_last_four;

// Output — An apple a day keeps anyone away if you t
echo $removed_last_twenty;
63

Bạn có thể sử dụng hàm ltrim[] nếu muốn xóa các ký tự cụ thể khỏi đầu chuỗi. Hàm này hoạt động giống như hàm

$sentence = "How are you feeling Today????!!!,,,..   ";

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence];

// Output — "How are you feeling Today????!!!,,,"
echo rtrim[$sentence, ' .'];

// Output — "How are you feeling Today????!!!,,,..."
echo rtrim[$sentence, ' ,'];

// Output — "How are you feeling Today????!!!"
echo rtrim[$sentence, ' .,'];

// Output — "How are you feeling Today"
echo rtrim[$sentence, '?!., '];
1 nhưng nó loại bỏ các ký tự ở đầu

PHP

$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_last_one   = substr[$sentence, 0, -1];
$removed_last_two   = substr[$sentence, 0, -2];
$removed_last_three = substr[$sentence, 0, -3];
$removed_last_four  = substr[$sentence, 0, -4];
$removed_last_twenty  = substr[$sentence, 0, -20];

// Output — An apple a day keeps anyone away if you throw it hard enough
echo $removed_last_one;

// Output — An apple a day keeps anyone away if you throw it hard enoug
echo $removed_last_two;

// Output — An apple a day keeps anyone away if you throw it hard enou
echo $removed_last_three;

// Output — An apple a day keeps anyone away if you throw it hard eno
echo $removed_last_four;

// Output — An apple a day keeps anyone away if you t
echo $removed_last_twenty;
6

Trong trường hợp đầu tiên, hàm

$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_last_one   = substr[$sentence, 0, -1];
$removed_last_two   = substr[$sentence, 0, -2];
$removed_last_three = substr[$sentence, 0, -3];
$removed_last_four  = substr[$sentence, 0, -4];
$removed_last_twenty  = substr[$sentence, 0, -20];

// Output — An apple a day keeps anyone away if you throw it hard enough
echo $removed_last_one;

// Output — An apple a day keeps anyone away if you throw it hard enoug
echo $removed_last_two;

// Output — An apple a day keeps anyone away if you throw it hard enou
echo $removed_last_three;

// Output — An apple a day keeps anyone away if you throw it hard eno
echo $removed_last_four;

// Output — An apple a day keeps anyone away if you t
echo $removed_last_twenty;
63 loại bỏ tất cả các
$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_last_one   = substr[$sentence, 0, -1];
$removed_last_two   = substr[$sentence, 0, -2];
$removed_last_three = substr[$sentence, 0, -3];
$removed_last_four  = substr[$sentence, 0, -4];
$removed_last_twenty  = substr[$sentence, 0, -20];

// Output — An apple a day keeps anyone away if you throw it hard enough
echo $removed_last_one;

// Output — An apple a day keeps anyone away if you throw it hard enoug
echo $removed_last_two;

// Output — An apple a day keeps anyone away if you throw it hard enou
echo $removed_last_three;

// Output — An apple a day keeps anyone away if you throw it hard eno
echo $removed_last_four;

// Output — An apple a day keeps anyone away if you t
echo $removed_last_twenty;
66 xuất hiện ở đầu câu hoặc chuỗi. Điều đáng ghi nhớ là chức năng này không dừng sau khi xóa một ký tự. Đó là lý do tại sao nó loại bỏ tất cả các ký tự dấu phẩy ở đầu và không chỉ dấu phẩy đầu tiên

Trong trường hợp thứ hai, hàm

$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_last_one   = substr[$sentence, 0, -1];
$removed_last_two   = substr[$sentence, 0, -2];
$removed_last_three = substr[$sentence, 0, -3];
$removed_last_four  = substr[$sentence, 0, -4];
$removed_last_twenty  = substr[$sentence, 0, -20];

// Output — An apple a day keeps anyone away if you throw it hard enough
echo $removed_last_one;

// Output — An apple a day keeps anyone away if you throw it hard enoug
echo $removed_last_two;

// Output — An apple a day keeps anyone away if you throw it hard enou
echo $removed_last_three;

// Output — An apple a day keeps anyone away if you throw it hard eno
echo $removed_last_four;

// Output — An apple a day keeps anyone away if you t
echo $removed_last_twenty;
63 đã loại bỏ tất cả các dấu chấm và dấu phẩy. Nó chỉ dừng lại khi đến dấu chấm than

Đầu ra trong trường hợp thứ ba và thứ tư có thể được giải thích tương tự

Tóm tắt nhanh

Hãy tóm tắt lại mọi thứ mà chúng tôi đã trình bày trong hướng dẫn này

  1. Bạn có thể sử dụng hàm
    $sentence = "An apple a day keeps anyone away if you throw it hard enough.";
    
    $removed_first_one   = substr[$sentence, 1];
    $removed_first_two   = substr[$sentence, 2];
    $removed_first_three = substr[$sentence, 3];
    $removed_first_four  = substr[$sentence, 4];
    $removed_first_twenty  = substr[$sentence, 20];
    
    // Output — "n apple a day keeps anyone away if you throw it hard enough."
    echo $removed_first_one;
    
    // Output — " apple a day keeps anyone away if you throw it hard enough."
    echo $removed_first_two;
    
    // Output — "apple a day keeps anyone away if you throw it hard enough."
    echo $removed_first_three;
    
    // Output — "pple a day keeps anyone away if you throw it hard enough."
    echo $removed_first_four;
    
    // Output — " anyone away if you throw it hard enough."
    echo $removed_first_twenty;
    4 nếu muốn xóa 'n' ký tự cuối cùng khỏi chuỗi. Tất cả những gì bạn phải làm là cung cấp giá trị âm cho
    $sentence = "How are you feeling Today????!!!,,,..   ";
    
    // Output — "How are you feeling Today????!!!,,,..."
    echo rtrim[$sentence];
    
    // Output — "How are you feeling Today????!!!,,,"
    echo rtrim[$sentence, ' .'];
    
    // Output — "How are you feeling Today????!!!,,,..."
    echo rtrim[$sentence, ' ,'];
    
    // Output — "How are you feeling Today????!!!"
    echo rtrim[$sentence, ' .,'];
    
    // Output — "How are you feeling Today"
    echo rtrim[$sentence, '?!., '];
    0
  2. Bạn cũng có thể xóa các ký tự 'n' đầu tiên khỏi chuỗi bằng hàm
    $sentence = "An apple a day keeps anyone away if you throw it hard enough.";
    
    $removed_first_one   = substr[$sentence, 1];
    $removed_first_two   = substr[$sentence, 2];
    $removed_first_three = substr[$sentence, 3];
    $removed_first_four  = substr[$sentence, 4];
    $removed_first_twenty  = substr[$sentence, 20];
    
    // Output — "n apple a day keeps anyone away if you throw it hard enough."
    echo $removed_first_one;
    
    // Output — " apple a day keeps anyone away if you throw it hard enough."
    echo $removed_first_two;
    
    // Output — "apple a day keeps anyone away if you throw it hard enough."
    echo $removed_first_three;
    
    // Output — "pple a day keeps anyone away if you throw it hard enough."
    echo $removed_first_four;
    
    // Output — " anyone away if you throw it hard enough."
    echo $removed_first_twenty;
    4. Lần này, bạn sẽ phải cung cấp một giá trị cho tham số
    $sentence = "An apple a day keeps anyone away if you throw it hard enough.";
    
    $removed_first_one   = substr[$sentence, 1];
    $removed_first_two   = substr[$sentence, 2];
    $removed_first_three = substr[$sentence, 3];
    $removed_first_four  = substr[$sentence, 4];
    $removed_first_twenty  = substr[$sentence, 20];
    
    // Output — "n apple a day keeps anyone away if you throw it hard enough."
    echo $removed_first_one;
    
    // Output — " apple a day keeps anyone away if you throw it hard enough."
    echo $removed_first_two;
    
    // Output — "apple a day keeps anyone away if you throw it hard enough."
    echo $removed_first_three;
    
    // Output — "pple a day keeps anyone away if you throw it hard enough."
    echo $removed_first_four;
    
    // Output — " anyone away if you throw it hard enough."
    echo $removed_first_twenty;
    9 và để nguyên
    $sentence = "How are you feeling Today????!!!,,,..   ";
    
    // Output — "How are you feeling Today????!!!,,,..."
    echo rtrim[$sentence];
    
    // Output — "How are you feeling Today????!!!,,,"
    echo rtrim[$sentence, ' .'];
    
    // Output — "How are you feeling Today????!!!,,,..."
    echo rtrim[$sentence, ' ,'];
    
    // Output — "How are you feeling Today????!!!"
    echo rtrim[$sentence, ' .,'];
    
    // Output — "How are you feeling Today"
    echo rtrim[$sentence, '?!., '];
    0
  3. Nếu bạn chỉ muốn xóa một số ký tự cụ thể khỏi đầu chuỗi, bạn có thể sử dụng hàm
    $sentence = "An apple a day keeps anyone away if you throw it hard enough.";
    
    $removed_last_one   = substr[$sentence, 0, -1];
    $removed_last_two   = substr[$sentence, 0, -2];
    $removed_last_three = substr[$sentence, 0, -3];
    $removed_last_four  = substr[$sentence, 0, -4];
    $removed_last_twenty  = substr[$sentence, 0, -20];
    
    // Output — An apple a day keeps anyone away if you throw it hard enough
    echo $removed_last_one;
    
    // Output — An apple a day keeps anyone away if you throw it hard enoug
    echo $removed_last_two;
    
    // Output — An apple a day keeps anyone away if you throw it hard enou
    echo $removed_last_three;
    
    // Output — An apple a day keeps anyone away if you throw it hard eno
    echo $removed_last_four;
    
    // Output — An apple a day keeps anyone away if you t
    echo $removed_last_twenty;
    63. Nó sẽ loại bỏ tất cả các lần xuất hiện của các ký tự đã cho bất kể chúng lặp lại bao nhiêu lần
  4. Nếu bạn muốn xóa một số ký tự cụ thể ở cuối chuỗi, bạn có thể sử dụng hàm
    $sentence = "How are you feeling Today????!!!,,,..   ";
    
    // Output — "How are you feeling Today????!!!,,,..."
    echo rtrim[$sentence];
    
    // Output — "How are you feeling Today????!!!,,,"
    echo rtrim[$sentence, ' .'];
    
    // Output — "How are you feeling Today????!!!,,,..."
    echo rtrim[$sentence, ' ,'];
    
    // Output — "How are you feeling Today????!!!"
    echo rtrim[$sentence, ' .,'];
    
    // Output — "How are you feeling Today"
    echo rtrim[$sentence, '?!., '];
    1. Nó cũng sẽ loại bỏ tất cả các lần xuất hiện của các ký tự đã cho bất kể chúng lặp lại bao nhiêu lần

Hãy cho tôi biết nếu có bất cứ điều gì bạn muốn tôi làm rõ trong hướng dẫn này. Ngoài ra, rất hoan nghênh bạn bình luận nếu bạn biết một số kỹ thuật khác để xóa một số ký tự nhất định khỏi đầu hoặc cuối chuỗi trong PHP

Chia sẻ với bạn bè

Đánh giá bài viết này -

[Chưa có xếp hạng]

Đang tải

Đặt mua

thông báo về

Nhãn mác

{} [+]

Tên*

E-mail*

Trang mạng

Nhãn mác

{} [+]

Tên*

E-mail*

Trang mạng

2 Nhận xét

Cũ nhất

Mới nhất Được bình chọn nhiều nhất

Phản hồi nội tuyến

Xem tất cả bình luận

tẩy rửa

2 năm trước

Xóa cả ký tự đầu tiên và ký tự cuối cùng

$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_first_one   = substr[$sentence, 1];
$removed_first_two   = substr[$sentence, 2];
$removed_first_three = substr[$sentence, 3];
$removed_first_four  = substr[$sentence, 4];
$removed_first_twenty  = substr[$sentence, 20];

// Output — "n apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_one;

// Output — " apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_two;

// Output — "apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_three;

// Output — "pple a day keeps anyone away if you throw it hard enough."
echo $removed_first_four;

// Output — " anyone away if you throw it hard enough."
echo $removed_first_twenty;
6

Ví dụ

$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_first_one   = substr[$sentence, 1];
$removed_first_two   = substr[$sentence, 2];
$removed_first_three = substr[$sentence, 3];
$removed_first_four  = substr[$sentence, 4];
$removed_first_twenty  = substr[$sentence, 20];

// Output — "n apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_one;

// Output — " apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_two;

// Output — "apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_three;

// Output — "pple a day keeps anyone away if you throw it hard enough."
echo $removed_first_four;

// Output — " anyone away if you throw it hard enough."
echo $removed_first_twenty;
7

đầu ra

$sentence = "An apple a day keeps anyone away if you throw it hard enough.";

$removed_first_one   = substr[$sentence, 1];
$removed_first_two   = substr[$sentence, 2];
$removed_first_three = substr[$sentence, 3];
$removed_first_four  = substr[$sentence, 4];
$removed_first_twenty  = substr[$sentence, 20];

// Output — "n apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_one;

// Output — " apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_two;

// Output — "apple a day keeps anyone away if you throw it hard enough."
echo $removed_first_three;

// Output — "pple a day keeps anyone away if you throw it hard enough."
echo $removed_first_four;

// Output — " anyone away if you throw it hard enough."
echo $removed_first_twenty;
8

Sửa đổi lần cuối cùng cách đây 2 năm bởi Nhóm Tutorialo

1

Hồi đáp

Quản trị viên

Nhóm hướng dẫn

2 năm trước

Trả lời

Cảm ơn sự đóng góp của bạn @kingwash. Tôi sẽ thêm kỹ thuật này vào bài viết chính ghi nhận bình luận của bạn. 🙂

Làm cách nào để xóa 2 chữ số đầu tiên khỏi số trong PHP?

Để loại bỏ n ký tự đầu tiên của chuỗi, chúng ta có thể sử dụng hàm substr[] có sẵn trong PHP. Hàm substr[] chấp nhận ba đối số, đối số đầu tiên là chuỗi, đối số thứ hai là vị trí bắt đầu, đối số thứ ba là độ dài [tùy chọn]. Trong ví dụ trên, chúng ta đã chuyển 2 làm đối số thứ hai cho hàm substr[]

Làm cách nào để lấy 3 ký tự đầu tiên của chuỗi trong PHP?

Để lấy n ký tự đầu tiên của một chuỗi, chúng ta có thể sử dụng hàm substr[] tích hợp sẵn trong PHP . Đây là một ví dụ, lấy 3 ký tự đầu tiên từ một chuỗi sau.

Làm cách nào để xóa hai ký tự cuối cùng khỏi chuỗi trong PHP?

Tùy chọn đầu tiên là Hàm Substr. Tùy chọn đầu tiên là sử dụng hàm substr. .
Tùy chọn thứ hai là Hàm Substr_replace. Tùy chọn thứ hai là sử dụng hàm substr_replace. .
Tùy chọn thứ ba là Hàm Rtrim[]. Chức năng này cũng cho phép loại bỏ ký tự cuối cùng của một chuỗi

Làm cách nào để xóa một ký tự khỏi chuỗi trong PHP?

Hàm trim[] trong PHP xóa khoảng trắng hoặc bất kỳ ký tự được xác định trước nào khác khỏi cả hai bên trái và phải của chuỗi. ltrim[] và rtrim[] được sử dụng để xóa các khoảng trắng này hoặc các ký tự khác khỏi bên trái và bên phải của chuỗi.

Chủ Đề