Hướng dẫn accessing individual characters in php - truy cập các ký tự riêng lẻ trong php

Hướng dẫn accessing individual characters in php - truy cập các ký tự riêng lẻ trong php

Hướng dẫn accessing individual characters in php - truy cập các ký tự riêng lẻ trong php
Hướng dẫn accessing individual characters in php - truy cập các ký tự riêng lẻ trong php

4.3. Truy cập các ký tự riêng lẻ

Hàm strlen () trả về số lượng ký tự trong một chuỗi:

$string = 'Hello, world';
$length = strlen($string);             // $length is 12

Bạn có thể sử dụng Cú pháp Array (được thảo luận chi tiết trong Chương 5) trên một chuỗi, để giải quyết các ký tự riêng lẻ:

$string = 'Hello';
for ($i=0; $i < strlen($string); $i++) {
  printf("The %dth character is %s\n", $i, $string[$i]);
}
The 0th character is H
The 1th character is e
The 2th character is l
The 3th character is l
The 4th character is o

Hướng dẫn accessing individual characters in php - truy cập các ký tự riêng lẻ trong php
Hướng dẫn accessing individual characters in php - truy cập các ký tự riêng lẻ trong php
Hướng dẫn accessing individual characters in php - truy cập các ký tự riêng lẻ trong php
4.2. Chuỗi in
Hướng dẫn accessing individual characters in php - truy cập các ký tự riêng lẻ trong php
4.4. Dây làm sạch


Hướng dẫn accessing individual characters in php - truy cập các ký tự riêng lẻ trong php

Bản quyền © 2003 O'Reilly & Associates. Đã đăng ký Bản quyền.

Truy cập các ký tự riêng lẻ

Hàm

$string = 'Hello';
for ($i=0; $i < strlen($string); $i++) {
  printf("The %dth character is %s\n", $i, $string[$i]);
}
The 0th character is H
The 1th character is e
The 2th character is l
The 3th character is l
The 4th character is o
0 trả về số lượng ký tự trong một chuỗi:

$string = 'Hello, world';
$length = strlen($string); // $length is 12

Bạn có thể sử dụng cú pháp bù chuỗi trên chuỗi để giải quyết các ký tự riêng lẻ:

$string = 'Hello';
for ($i=0; $i < strlen($string); $i++) {
  printf("The %dth character is %s\n", $i, $string{$i});
}
The 0th character is H
The 1th character is e
The 2th character is l
The 3th character is l
The 4th character is o

Nhận lập trình PHP, phiên bản thứ 3 ngay bây giờ với nền tảng học tập O hèReilly.learning platform.

Các thành viên của O hèReilly trải nghiệm đào tạo trực tuyến trực tiếp, cộng với sách, video và nội dung kỹ thuật số từ gần 200 nhà xuất bản.nearly 200 publishers.

Truy cập các ký tự riêng lẻ

Hàm

$string = 'Hello';
for ($i=0; $i < strlen($string); $i++) {
  printf("The %dth character is %s\n", $i, $string[$i]);
}
The 0th character is H
The 1th character is e
The 2th character is l
The 3th character is l
The 4th character is o
1 trả về số lượng ký tự trong một chuỗi:

$string = 'Hello, world';
$length = strlen($string);             // $length is 12

Bạn có thể sử dụng Cú pháp Array (được thảo luận chi tiết trong Chương 5) trên một chuỗi, để giải quyết các ký tự riêng lẻ:

$string = 'Hello';
for ($i=0; $i < strlen($string); $i++) {
  printf("The %dth character is %s\n", $i, $string[$i]);
}
The 0th character is H
            
The 1th character is e
            
The 2th character is l
            
The 3th character is l
            
The 4th character is o

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

    Bàn luậnstr_split() method – The str_split() method is used to split the specified string variable into an array of values, each of which is mapped to an index value beginning with 0. This method converts the input string into an array.

    str_split(str)

    Một chuỗi là một chuỗi các ký tự. Nó có thể chứa số nguyên hoặc thậm chí các biểu tượng đặc biệt. Mỗi ký tự trong một chuỗi được lưu trữ ở một vị trí duy nhất được biểu thị bằng một giá trị chỉ mục duy nhất. & NBSP;

    Example:

    Cách tiếp cận 1: Sử dụng phương thức str_split () - Phương thức str_split () được sử dụng để chia biến chuỗi được chỉ định thành một mảng các giá trị, mỗi trong số đó được ánh xạ tới giá trị chỉ mục bắt đầu bằng 0. Phương thức này chuyển đổi chuỗi đầu vào thành một mảng .

    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    2

    & nbsp; lặp lại vòng lặp foreach sau đó có thể được thực hiện trên các giá trị mảng, mỗi phần tử thuộc về một ký tự của chuỗi. Các giá trị sau đó được in với một khoảng trống ở giữa mỗi. & NBSP;

    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    7
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    8
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    9
    $string = 'Hello, world';
    $length = strlen($string); // $length is 12
    0

    PHP

    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    3
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    4
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    5
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    6

    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    7
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    8
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string{$i});
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    5
    $string = 'Hello, world';
    $length = strlen($string); // $length is 12
    0

    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    7
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    8
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    3
    $string = 'Hello, world';
    $length = strlen($string); // $length is 12
    4
    $string = 'Hello, world';
    $length = strlen($string); // $length is 12
    5
    $string = 'Hello, world';
    $length = strlen($string); // $length is 12
    0

    $string = 'Hello, world';
    $length = strlen($string); // $length is 12
    7
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    4
    $string = 'Hello, world';
    $length = strlen($string); // $length is 12
    9
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    8
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    3
    $string = 'Hello, world';
    $length = strlen($string); // $length is 12
    0

    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
                
    The 1th character is e
                
    The 2th character is l
                
    The 3th character is l
                
    The 4th character is o
    0

    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
                
    The 1th character is e
                
    The 2th character is l
                
    The 3th character is l
                
    The 4th character is o
    1

    Output:

    Original string : Hi!GFG User.
    Characters : H i ! G F G U s e r .

    Cách tiếp cận 2: Sử dụng phương thức strlen () - Thestrlen () được sử dụng để tính độ dài của chuỗi được chỉ định trong PHP. Một lần lặp vòng lặp được áp dụng bằng cách sử dụng độ dài của chuỗi và ký tự chỉ mục ith được in mỗi lần. Độ phức tạp thời gian giống như phương pháp trước đó. Tuy nhiên, không cần thêm không gian để lưu trữ chuỗi dưới dạng đối tượng mảng. & NBSP;Thestrlen()method is used to compute the length of the specified string in PHP. A for loop iteration is applied using the length of the string, and the ith index character is printed each time. The time complexity is the same as the previous method. However, no extra space is required to store the string in the form of the array object. 

    strlen(str)

    Example:

    PHP

    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    2

    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    3
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    4
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    5
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    6

    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    7
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    8
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    9
    $string = 'Hello, world';
    $length = strlen($string); // $length is 12
    0

    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    7
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    8
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    3
    str_split(str)
    4
    $string = 'Hello, world';
    $length = strlen($string); // $length is 12
    5
    $string = 'Hello, world';
    $length = strlen($string); // $length is 12
    0

    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    7
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    8
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string{$i});
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    5
    $string = 'Hello, world';
    $length = strlen($string); // $length is 12
    0

    Original string : Hi!GFG User.
    Characters : H i ! G F G U s e r .
    1
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    4
    Original string : Hi!GFG User.
    Characters : H i ! G F G U s e r .
    3
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    8
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    3
    $string = 'Hello, world';
    $length = strlen($string); // $length is 12
    0

    Original string : Hi!GFG User.
    Characters : H i ! G F G U s e r .
    7
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    8__

    $string = 'Hello, world';
    $length = strlen($string);             // $length is 12
    3
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    7
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    8
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    3
    Original string : Hi!GFG User.
    Characters : H i ! G F G U s e r .
    1
    Original string : Hi!GFG User.
    Characters : H i ! G F G U s e r .
    9
    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string{$i});
    }
    The 0th character is H
    The 1th character is e
    The 2th character is l
    The 3th character is l
    The 4th character is o
    $string = 'Hello, world';
    $length = strlen($string);             // $length is 12
    8
    $string = 'Hello, world';
    $length = strlen($string); // $length is 12
    0

    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
                
    The 1th character is e
                
    The 2th character is l
                
    The 3th character is l
                
    The 4th character is o
    0

    $string = 'Hello';
    for ($i=0; $i < strlen($string); $i++) {
      printf("The %dth character is %s\n", $i, $string[$i]);
    }
    The 0th character is H
                
    The 1th character is e
                
    The 2th character is l
                
    The 3th character is l
                
    The 4th character is o
    1

    Output:

    Original string : Hi!GFG User.
    Characters : H i ! G F G U s e r .

    Làm thế nào tôi có thể truy cập các ký tự riêng lẻ trong một chuỗi trong PHP?

    Mỗi ký tự trong một chuỗi được lưu trữ ở một vị trí duy nhất được biểu thị bằng một giá trị chỉ mục duy nhất.Cách tiếp cận 1: Sử dụng phương thức str_split () - Phương thức str_split () được sử dụng để phân chia biến chuỗi được chỉ định thành một mảng các giá trị, mỗi trong đó được ánh xạ tới giá trị chỉ mục bắt đầu bằng 0.Using str_split() method – The str_split() method is used to split the specified string variable into an array of values, each of which is mapped to an index value beginning with 0.

    Làm thế nào tôi có thể nhận được các chữ cái từ chuỗi trong PHP?

    $ result = preg_replace ("/[^a-za-z0-9]+/", "", $ s);Nếu định nghĩa của bạn về chữ và số của bạn bao gồm các chữ cái bằng tiếng nước ngoài và các tập lệnh lỗi thời thì bạn sẽ cần sử dụng các lớp ký tự Unicode.Hãy thử điều này để chỉ để lại a-z: $ result = preg_replace ("/[^a-z]+/", "", $ s); If your definition of alphanumeric includes letters in foreign languages and obsolete scripts then you will need to use the Unicode character classes. Try this to leave only A-Z: $result = preg_replace("/[^A-Z]+/", "", $s);

    Làm cách nào để nhận được chữ cái đầu tiên của một từ trong PHP?

    Vì vậy, bạn có thể dễ dàng sử dụng $ I [0] để tìm nạp chữ cái đầu tiên.$i[0] to fetch first letter.

    Chất nền trong PHP là gì?

    Chất nền trong PHP là một hàm tích hợp được sử dụng để trích xuất một phần của chuỗi đã cho.Hàm trả về chuỗi con được chỉ định bởi tham số bắt đầu và độ dài.Nó được hỗ trợ bởi PHP 4 trở lên.a built-in function used to extract a part of the given string. The function returns the substring specified by the start and length parameter. It is supported by PHP 4 and above.