Hướng dẫn dùng strspn trong PHP

Hàm strspn() sẽ đếm số kí tự liên tiếp từ đầu chuỗi nguồn có tồn tại trong danh sách các kí tự truyền vào, hàm sẽ đếm từ đầu chuỗi và dừng lại khi gặp kí bất kì kí tự nào không tồn tại trong danh sách truyền vào.

Hướng dẫn dùng strspn trong PHP

Hướng dẫn dùng strspn trong PHP

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Hàm trả về số nguyên là số kí tự tồn tại trong danh sách các kí tự truyền vào, và xuất hiện liên tiếp từ đầu chuỗi nguồn.

Cú pháp

Cú phápstrspn( $str, $listchar, $start, $lent);

Trong đó:

Bài viết này được đăng tại [free tuts .net]

  • $str là chuỗi nguồn.
  • $listchar là danh sách các kí tự cần tìm kiếm.
  • $start là vị trí bắt đầu tìm kiếm trong chuỗi $str, các vị trí trước $start sẽ không được hàm strspn() tìm kiếm.Mặc định $start = 0.
  • $lent số kí tự mà hàm strspn() sẽ tìm kiếm trong chuỗi $str, Nếu không truyền hàm sẽ tìm đến hết chuỗi.

Ví dụ

Code

$str = "abcd efgh iklm";
echo strspn($str, 'ab') . "
"; echo strspn($str, 'abcd') . "
"; echo strspn($str, 'abcd', 2) . "
"; echo strspn($str, 'abcd', 0, 2) . "
";

Tham khảo: php.net

  • Trang chủ
  • Phát triển web
  • PHP
  • Hàm strspn() trong PHP

Hướng dẫn cách sử dụng hàm strspn() trong lập trình PHP

Tác dụng của hàm strspn()

The strspn() function returns the number of characters present in the initial part of the string that contains only characters specified in the mask (list of allowable characters).

The following table summarizes the technical details of this function.

Return Value:Returns the length of the initial portion of the string that consist entirely of characters contained within the given mask.
Version:PHP 4+


Syntax

The basic syntax of the strspn() function is given with:

strspn(string, mask, start, length);

The following example shows the strspn() function in action.


Parameters

The strspn() function accepts the following parameters.

ParameterDescription
string Required. Specifies the string to work on.
mask Required. Specifies the string containing every allowable characters.
start Optional. Specifies the position in the string to start searching.
length Optional. Specifies the portion of the string to search.


More Examples

Here're some more examples showing how strspn() function actually works:

If string does not start with any characters specified in the mask, this function returns 0.

The following example returns the length of the initial portion of the string that consists only of characters contained within "Her". Let's try it out and see how it works:

In the following example only the portion "World" of the string will be examined, because the position to start searching is 6 and the length of the string to search is 5.

Bài viết này đã giúp ích cho bạn?

Bài viết mới

Hàm strspn() sẽ đếm số kí tự liên tiếp từ đầu chuỗi nguồn có tồn tại trong danh sách các kí tự truyền vào, hàm sẽ đếm từ đầu chuỗi và dừng lại khi gặp kí bất kì kí tự nào không tồn tại trong danh sách truyền vào.

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Hàm trả về số nguyên là số kí tự tồn tại trong danh sách các kí tự truyền vào, và xuất hiện liên tiếp từ đầu chuỗi nguồn.

Cú pháp

Cú phápstrspn( $str, $listchar, $start, $lent);

Trong đó:

Bài viết này được đăng tại [free tuts .net]

  • $str là chuỗi nguồn.
  • $listchar là danh sách các kí tự cần tìm kiếm.
  • $start là vị trí bắt đầu tìm kiếm trong chuỗi $str, các vị trí trước $start sẽ không được hàm strspn() tìm kiếm.Mặc định $start = 0.
  • $lent số kí tự mà hàm strspn() sẽ tìm kiếm trong chuỗi $str, Nếu không truyền hàm sẽ tìm đến hết chuỗi.

Ví dụ

Code

$str = "abcd efgh iklm";
echo strspn($str, 'ab') . "
"; echo strspn($str, 'abcd') . "
"; echo strspn($str, 'abcd', 2) . "
"; echo strspn($str, 'abcd', 0, 2) . "
";

Tham khảo: php.net

  • Trang chủ
  • Phát triển web
  • PHP
  • Hàm strspn() trong PHP

Hướng dẫn cách sử dụng hàm strspn() trong lập trình PHP

Tác dụng của hàm strspn()

The strspn() function returns the number of characters present in the initial part of the string that contains only characters specified in the mask (list of allowable characters).

The following table summarizes the technical details of this function.

Return Value:Returns the length of the initial portion of the string that consist entirely of characters contained within the given mask.
Version:PHP 4+

Syntax

The basic syntax of the strspn() function is given with:

strspn(string, mask, start, length);

The following example shows the strspn() function in action.


Parameters

The strspn() function accepts the following parameters.

ParameterDescription
string Required. Specifies the string to work on.
mask Required. Specifies the string containing every allowable characters.
start Optional. Specifies the position in the string to start searching.
length Optional. Specifies the portion of the string to search.

More Examples

Here're some more examples showing how strspn() function actually works:

If string does not start with any characters specified in the mask, this function returns 0.

The following example returns the length of the initial portion of the string that consists only of characters contained within "Her". Let's try it out and see how it works:

In the following example only the portion "World" of the string will be examined, because the position to start searching is 6 and the length of the string to search is 5.

Bài viết này đã giúp ích cho bạn?

Bài viết mới

❮ Tham chiếu chuỗi PHP

Thí dụ

Trả về số ký tự được tìm thấy trong chuỗi "Hello world!" có chứa các ký tự "kHlleo":

echo strspn("Hello world!","kHlleo");
?>


Định nghĩa và Cách sử dụng

Hàm strspn () trả về số lượng ký tự được tìm thấy trong chuỗi chỉ chứa các ký tự từ tham số charlist.

Mẹo: Sử dụng hàm strcspn () để trả về số ký tự được tìm thấy trong một chuỗi trước khi tìm thấy bất kỳ phần nào của các ký tự được chỉ định.

Lưu ý: Hàm này là an toàn nhị phân.


Cú pháp

strspn(string,charlist,start,length)

Giá trị tham số

ParameterDescription
string Required. Specifies the string to search
charlist Required. Specifies the characters to find
start Optional. Specifies where in the string to start
length Optional. Defines the length of the string

Chi tiết kỹ thuật

Giá trị trả lại:Trả về số ký tự được tìm thấy trong chuỗi chỉ chứa các ký tự từ tham số danh sách ký tự.
Phiên bản PHP:4+
Changelog:Các tham số bắt đầuchiều dài đã được thêm vào trong PHP 4.3.

Các ví dụ khác

Thí dụ

Trả về số ký tự được tìm thấy trong chuỗi "abcdefand" có chứa các ký tự "abc":

echo strspn("abcdefand","abc");
?>


❮ Tham chiếu chuỗi PHP