Hướng dẫn get last word php - lấy từ php cuối cùng

Nếu bạn theo sau từ cuối cùng trong một câu, tại sao không làm điều gì đó như thế này?

Tôi sẽ không khuyên bạn nên sử dụng các biểu thức thông thường vì nó không cần thiết, trừ khi bạn thực sự muốn vì một số lý do.

Sử dụng phương pháp

$string = 'Retrieving the last word of a string using PHP.';
$last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
$last_word = substr($string, $last_word_start); // $last_word = PHP.
1 sẽ tốt hơn cả hai nếu tài nguyên/hiệu quả/chi phí là một mối quan tâm.

Nó nhanh hơn, mặc dù nó thực sự không tạo ra nhiều sự khác biệt về những thứ như thế này. Nếu bạn liên tục cần biết từ cuối cùng trên chuỗi 100.000 từ, có lẽ bạn nên nói về nó theo một cách khác.

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Input : "php exercises" 
    Output : 9
    
    Input : "geeks for geeks"
    Output : 5

    Bàn luận

    • Chúng tôi được cung cấp một chuỗi. Chúng tôi được yêu cầu viết một chương trình trong PHP để tìm độ dài của từ cuối cùng trong chuỗi bằng cách sử dụng các hàm sẵn có. Chúng tôi đã thảo luận về cách tiếp cận giải quyết vấn đề này ở đây. Bài viết này thảo luận về giải pháp PHP cho vấn đề. Ví dụ:: This inbuilt function in PHP is used to extract a part of string.: This inbuilt function in PHP is used to extract a part of string.
    • Chúng tôi sẽ chủ yếu sử dụng ba hàm sẵn này trong PHP để giải quyết vấn đề này:: This inbuilt function in PHP is used to find the last position of string in original or in another string. It returns the integer value corresponding to position of last occurrence of the string, also it treats uppercase and lowercase characters uniquely.: This inbuilt function in PHP is used to find the last position of string in original or in another string. It returns the integer value corresponding to position of last occurrence of the string, also it treats uppercase and lowercase characters uniquely.
    • Chức năng con (): Hàm sự sẵn có này trong PHP được sử dụng để trích xuất một phần của chuỗi.: This inbuilt function in PHP is used to find the length of a string.: This inbuilt function in PHP is used to find the length of a string.

    Chức năng strrpos (): Hàm Inbuilt này trong PHP được sử dụng để tìm vị trí cuối cùng của chuỗi trong bản gốc hoặc trong một chuỗi khác. Nó trả về giá trị số nguyên tương ứng với vị trí xuất hiện cuối cùng của chuỗi, nó cũng xử lý các ký tự chữ hoa và chữ thường duy nhất.

    Chức năng strlen (): Hàm Inbuilt này trong PHP được sử dụng để tìm độ dài của một chuỗi.

    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    2

    Ý tưởng để giải quyết vấn đề này bằng cách sử dụng hàm Inbuilt được đề cập ở trên là trước tiên tìm vị trí của không gian xuất hiện cuối cùng trong chuỗi bằng hàm strrpos (). Sau khi nhận được vị trí của không gian xuất hiện cuối cùng, chúng ta có thể dễ dàng có được từ cuối cùng trong chuỗi bằng cách sử dụng hàm con () và lưu trữ điều này trong một biến chuỗi mới. Cuối cùng, chúng ta có thể sử dụng hàm strlen () để tìm độ dài của từ cuối cùng trong chuỗi. & Nbsp;

    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    0
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    6

    PHP

    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    7
    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    7
    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    8
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    8
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    4
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    7
    Input : "php exercises" 
    Output : 9
    
    Input : "geeks for geeks"
    Output : 5
    2
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    0____11
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    2
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    3
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    4
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    7
    Input : "php exercises" 
    Output : 9
    
    Input : "geeks for geeks"
    Output : 5
    7
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    7
    Input : "php exercises" 
    Output : 9
    
    Input : "geeks for geeks"
    Output : 5
    9
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    7
    Input : "php exercises" 
    Output : 9
    
    Input : "geeks for geeks"
    Output : 5
    2
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    7
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    8
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    9
    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    0
    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    1
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    3
    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    3
    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    4
    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    5
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    7
    Input : "php exercises" 
    Output : 9
    
    Input : "geeks for geeks"
    Output : 5
    7
    Input : "php exercises" 
    Output : 9
    
    Input : "geeks for geeks"
    Output : 5
    3
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    8
    Input : "php exercises" 
    Output : 9
    
    Input : "geeks for geeks"
    Output : 5
    5
    Input : "php exercises" 
    Output : 9
    
    Input : "geeks for geeks"
    Output : 5
    3
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    8
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    9
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    8
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    86
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    0
    Input : "php exercises" 
    Output : 9
    
    Input : "geeks for geeks"
    Output : 5
    7
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    0
    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    27
    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    28
    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    29
    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    0
    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    27
    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    72
    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    29
    $string = 'Retrieving the last word of a string using PHP.';
    $last_word_start = strrpos($string, ' ') + 1; // +1 so we don't include the space in our result
    $last_word = substr($string, $last_word_start); // $last_word = PHP.
    
    74

    Output:

    $string = 'Retrieving the last word of a string using PHP.';
    preg_match('/[^ ]*$/', $string, $results);
    $last_word = $results[0]; // $last_word = PHP.
    
    8

    Kiểm tra kỹ năng lập trình của bạn với bài kiểm tra của W3Resource.

    Có vẻ như bạn gần như biết những gì bạn muốn làm, về cơ bản bạn đã định nghĩa nó là một regex.

    Hàm strrpos () tìm thấy vị trí của lần xuất hiện cuối cùng của một chuỗi bên trong chuỗi khác.strrpos() function finds the position of the last occurrence of a string inside another string.strrpos() function finds the position of the last occurrence of a string inside another string.

    Phương pháp 3: Sử dụng hàm strstr (): hàm strstr () được sử dụng để tìm kiếm lần xuất hiện đầu tiên của chuỗi bên trong chuỗi khác.Hàm này là nhạy cảm trường hợp..strstr ($ câu, '', đúng);Using strstr() Function: The strstr() function is used to search the first occurrence of a string inside another string. This function is case-sensitive. . strstr ( $sentence , ' ' , true);Using strstr() Function: The strstr() function is used to search the first occurrence of a string inside another string. This function is case-sensitive. . strstr ( $sentence , ' ' , true);

    Giải pháp mẫu: Mã PHP:echo preg_replace('/\W\w+\s*(\W*)$/', '$1', $str1).echo preg_replace('/\W\w+\s*(\W*)$/', '$1', $str1).