Hướng dẫn strpbrk php

(PHP 5, PHP 7, PHP 8)

strpbrkSearch a string for any of a set of characters

Description

strpbrk(string $string, string $characters): string|false

Parameters

string

The string where characters is looked for.

characters

This parameter is case sensitive.

Return Values

Returns a string starting from the character found, or false if it is not found.

Examples

Example #1 strpbrk() example

$text

'This is a Simple text.';// this echoes "is is a Simple text." because 'i' is matched first
echo strpbrk($text'mi');// this echoes "Simple text." because chars are case sensitive
echo strpbrk($text'S');
?>

See Also

  • strpos() - Find the position of the first occurrence of a substring in a string
  • strstr() - Find the first occurrence of a string
  • preg_match() - Perform a regular expression match

devnuhl

8 years ago

If you're not looking to duplicate the rest of the string, but instead just want the offset, in the spirit of the str*pos() functions, use strcspn()

guillaume dot barranco at free dot fr

4 years ago

A little modification to Evan's code to use an array for the second parameter :

function strpbrkpos($s, $accept) {
 
$r = FALSE;
 
$t = 0;
 
$i = 0;
 
$accept_l = count($accept);

  for ( ;

$i < $accept_l ; $i++ )
    if ( (
$t = strpos($s, $accept[$i])) !== FALSE )
      if ( (
$r === FALSE) || ($t < $r) )
       
$r = $t;

    return

$r;
}
?>

❮ Tham chiếu chuỗi PHP

Thí dụ

Tìm kiếm một chuỗi cho các ký tự "oe" và trả về phần còn lại của chuỗi mà từ đó nó tìm thấy lần xuất hiện đầu tiên của các ký tự được chỉ định:

echo strpbrk("Hello world!","oe");
?>


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

Hàm strpbrk () tìm kiếm một chuỗi cho bất kỳ ký tự nào được chỉ định.

Lưu ý: Hàm này phân biệt chữ hoa chữ thường.

Hàm này trả về phần còn lại của chuỗi từ nơi nó tìm thấy lần xuất hiện đầu tiên của một ký tự được chỉ định, nếu không, nó trả về FALSE.


Cú pháp

Giá trị tham số

ParameterDescription
string Required. Specifies the string to search
charlist Required. Specifies the characters to find

Chi tiết kỹ thuật

Giá trị trả lại:Trả về chuỗi bắt đầu từ ký tự được tìm thấy, nếu không, nó trả về FALSE
Phiên bản PHP:5+

Các ví dụ khác

Thí dụ

Hàm này phân biệt chữ hoa chữ thường ("W" và "w" sẽ không xuất ra giống nhau):

echo strpbrk("Hello world!","W");
echo "
";
echo strpbrk("Hello world!","w");
?>


❮ Tham chiếu chuỗi PHP