Hướng dẫn write a php script to get the last three characters of a string. - viết một tập lệnh php để lấy ba ký tự cuối cùng của một chuỗi.

Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:50:37 (UTC/GMT +8 giờ)

Chuỗi PHP: Tập thể dục-7 với giải pháp

Viết tập lệnh PHP để có được ba ký tự cuối cùng của một chuỗi.

Chuỗi mẫu: '[Email & nbsp; được bảo vệ]'

Trình bày bằng hình ảnh:

Hướng dẫn write a php script to get the last three characters of a string. - viết một tập lệnh php để lấy ba ký tự cuối cùng của một chuỗi.

Giải pháp mẫu:

Mã PHP:



Đầu ra mẫu:

com

Sơ đồ:

Hướng dẫn write a php script to get the last three characters of a string. - viết một tập lệnh php để lấy ba ký tự cuối cùng của một chuỗi.

Trình chỉnh sửa mã PHP:

Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn (và nhận xét) thông qua Disqus.

Trước đây: Viết tập lệnh PHP để trích xuất tên người dùng từ id email được chỉ định. Write a PHP script to extract the user name from the specified email ID.
Next: Write a PHP script to format values in currency style.

PHP: Lời khuyên trong ngày

PHP: Sử dụng str_replace để nó chỉ hành động trong trận đấu đầu tiên?

Có thể được thực hiện với preg_replace:

function str_replace_first($from, $to, $content)
{
    $from = '/'.preg_quote($from, '/').'/';

    return preg_replace($from, $to, $content, 1);
}

echo str_replace_first('abc', '123', 'abcdef abcdef abcdef'); 
// outputs '123def abcdef abcdef'

Phép thuật nằm trong tham số thứ tư tùy chọn [giới hạn]. Từ tài liệu:

[Giới hạn] - Sự thay thế tối đa có thể cho mỗi mẫu trong mỗi chuỗi chủ đề. Mặc định là -1 (không có giới hạn).

Tham khảo: https://bit.ly/35Scup8

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

    Examples:

    Input : $str = "GeeksforGeeks!"
            $n = 6 
    Output : Geeks!
    
    Input : $str = "GeeksforGeeks!"
            $n = 9
    Output : forGeeks!
    

    Bàn luận Tn this method, traverse the last N characters of the string and keep appending them in a new string.

    Example:

    Viết một chương trình PHP để có được n ký tự cuối cùng của một chuỗi đã cho.

    Phương pháp 1: TN Phương pháp này, Traverse N Các ký tự cuối cùng của chuỗi và tiếp tục nối chúng trong một chuỗi mới.

    $str = "GeeksforGeeks!"

    com
    
    0

    com
    
    1
    com
    
    2

    com
    
    3 =
    com
    
    5
    com
    
    6____________
    com
    
    8
    com
    
    1

    function str_replace_first($from, $to, $content)
    {
        $from = '/'.preg_quote($from, '/').'/';
    
        return preg_replace($from, $to, $content, 1);
    }
    
    echo str_replace_first('abc', '123', 'abcdef abcdef abcdef'); 
    // outputs '123def abcdef abcdef'
    
    1 =
    function str_replace_first($from, $to, $content)
    {
        $from = '/'.preg_quote($from, '/').'/';
    
        return preg_replace($from, $to, $content, 1);
    }
    
    echo str_replace_first('abc', '123', 'abcdef abcdef abcdef'); 
    // outputs '123def abcdef abcdef'
    
    3
    com
    
    0

    6

    function str_replace_first($from, $to, $content)
    {
        $from = '/'.preg_quote($from, '/').'/';
    
        return preg_replace($from, $to, $content, 1);
    }
    
    echo str_replace_first('abc', '123', 'abcdef abcdef abcdef'); 
    // outputs '123def abcdef abcdef'
    
    5
    com
    
    6
    function str_replace_first($from, $to, $content)
    {
        $from = '/'.preg_quote($from, '/').'/';
    
        return preg_replace($from, $to, $content, 1);
    }
    
    echo str_replace_first('abc', '123', 'abcdef abcdef abcdef'); 
    // outputs '123def abcdef abcdef'
    
    7 =
    com
    
    3__

    /** Unlike substr, handles case where $string is too short.
     * @param $string
     * @param $nChars - negative to return at end of string.
     */
    function safe_substr($string, $nChars) {
        if ($nChars == 0 || !isset($string))
            return "";
    
        if (strlen($string) <= abs($nChars))
            // $string is too short (or exactly the desired length). Return the string.
            return $string;
    
        return substr($string, $nChars);
    }
    
    0

    Input : $str = "GeeksforGeeks!"
            $n = 6 
    Output : Geeks!
    
    Input : $str = "GeeksforGeeks!"
            $n = 9
    Output : forGeeks!
    
    9
    function str_replace_first($from, $to, $content)
    {
        $from = '/'.preg_quote($from, '/').'/';
    
        return preg_replace($from, $to, $content, 1);
    }
    
    echo str_replace_first('abc', '123', 'abcdef abcdef abcdef'); 
    // outputs '123def abcdef abcdef'
    
    1
    1$str
    3
    function str_replace_first($from, $to, $content)
    {
        $from = '/'.preg_quote($from, '/').'/';
    
        return preg_replace($from, $to, $content, 1);
    }
    
    echo str_replace_first('abc', '123', 'abcdef abcdef abcdef'); 
    // outputs '123def abcdef abcdef'
    
    7
    5
    Another way to do this use inbuilt library function substr with parameters as the name of the string.

    Example:

    $str = "GeeksforGeeks!"

    com
    
    0

    com
    
    1
    com
    
    2

    com
    
    3 =
    com
    
    5
    com
    
    6____________
    com
    
    8
    com
    
    1

    function str_replace_first($from, $to, $content)
    {
        $from = '/'.preg_quote($from, '/').'/';
    
        return preg_replace($from, $to, $content, 1);
    }
    
    echo str_replace_first('abc', '123', 'abcdef abcdef abcdef'); 
    // outputs '123def abcdef abcdef'
    
    1 = 8
    com
    
    6____________$str1
    com
    
    3$str3

    7 
    function str_replace_first($from, $to, $content)
    {
        $from = '/'.preg_quote($from, '/').'/';
    
        return preg_replace($from, $to, $content, 1);
    }
    
    echo str_replace_first('abc', '123', 'abcdef abcdef abcdef'); 
    // outputs '123def abcdef abcdef'
    
    1
    com
    
    0

    /** Unlike substr, handles case where $string is too short.
     * @param $string
     * @param $nChars - negative to return at end of string.
     */
    function safe_substr($string, $nChars) {
        if ($nChars == 0 || !isset($string))
            return "";
    
        if (strlen($string) <= abs($nChars))
            // $string is too short (or exactly the desired length). Return the string.
            return $string;
    
        return substr($string, $nChars);
    }
    
    0

    Lưu ý: Trong ví dụ trên, $ start cũng có thể mất -n để tạo chuỗi con của n ký tự cuối cùng In above example, $start can also take -N to create a sub-string of last n characters


    Giới thiệu

    PHP là ngôn ngữ kịch bản máy chủ và nó là một công cụ mạnh mẽ để tạo các trang web tương tác và động. Tôi đã sử dụng Wamperver 2.2 cho ví dụ sau.

    Kết quả

    Hướng dẫn write a php script to get the last three characters of a string. - viết một tập lệnh php để lấy ba ký tự cuối cùng của một chuỗi.
    Viết tập lệnh PHP để lấy ba ký tự cuối cùng của chuỗi

    Có nhiều câu trả lời đúng ở đây. Nhưng không rõ ràng những gì cần thiết, nếu bạn muốn phiên bản "an toàn" của 8,

    Giống như 8, khi chuỗi "đủ dài", nhưng nếu chuỗi quá ngắn, hãy trả lại chuỗi gốc (thay vì trả về = 0).

    /** Unlike substr, handles case where $string is too short.
     * @param $string
     * @param $nChars - negative to return at end of string.
     */
    function safe_substr($string, $nChars) {
        if ($nChars == 0 || !isset($string))
            return "";
    
        if (strlen($string) <= abs($nChars))
            // $string is too short (or exactly the desired length). Return the string.
            return $string;
    
        return substr($string, $nChars);
    }
    

    Lưu ý: Đối với UTF-8 chars, xác định = 1, thay thế 8 ở trên bằng = 3. Và thay thế

    com
    
    5 bằng = 5.

    Làm thế nào tôi có thể nhận được ký tự cuối cùng của một chuỗi trong PHP?

    Sử dụng Phương thức Subr (): Subr () là hàm tích hợp trong PHP được sử dụng để trích xuất một phần của chuỗi. Ví dụ: Ví dụ: nếu chuỗi là Akshit yêu thích GeekSforGeeks. Ký tự cuối cùng của chuỗi là về S S S.: The substr() is a built-in function in PHP that is used to extract a part of string. Example: For example, if the string is “Akshit loves GeeksForGeeks”. The last character of the string is “s”.

    Làm thế nào tôi có thể nhận được 3 ký tự đầu tiên của một chuỗi trong PHP?

    Để có được n ký tự đầu tiên của chuỗi, chúng ta có thể sử dụng hàm Subr () tích hợp trong PHP.Dưới đây là một ví dụ, nhận được 3 ký tự đầu tiên từ một chuỗi sau:use the built-in substr() function in PHP. Here is an example, that gets the first 3 characters from a following string:

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

    Làm cách nào để loại bỏ 4 ký tự cuối cùng khỏi một chuỗi ?..
    Sử dụng StringBuffer.Lớp Deletecahrat () ..
    Sử dụng chuỗi.Phương thức chuỗi con () ..
    Sử dụng StringUtils.Phương thức Chop () ..
    Sử dụng biểu thức chính quy ..

    Làm thế nào tôi có thể nhận các ký tự cụ thể trong một chuỗi trong PHP?

    Từ tài liệu PHP: Các ký tự trong các chuỗi có thể được truy cập và sửa đổi bằng cách chỉ định độ lệch dựa trên 0 của ký tự mong muốn sau chuỗi sử dụng khung mảng vuông, như trong $ str [42].Hãy nghĩ về một chuỗi như một mảng các ký tự cho mục đích này.specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42] . Think of a string as an array of characters for this purpose.