PHP – iconv_strpos() 函式 – 在字串中查詢某個字元第一次出現的位置


在 PHP 中,**iconv_strpos()** 函式用於讀取給定字串的第一個字元。它查詢字串中某個字元第一次出現的位置。它是 PHP 中的一個內建函式。

語法

string iconv_strpos(string $haystack, string $needle, int $offset, string $encoding)

**注意:strpos()**,iconv_strpos() 的返回值是出現在搜尋目標之前的字元數,而不是找到搜尋目標的位元組偏移量。字元計數基於指定的字元集編碼。

引數

**iconv_strpos()** 函式接受四個不同的引數 - **$haystack**、**$needle**、**$offset** 和 **$encoding**。

  • **$haystack−** 它表示整個字串。

  • **$needle−** $needle 引數用於在給定的整個字串中搜索子字串。

  • **$offset−** $offset 引數是可選的,用於指定搜尋應該從哪個位置開始。如果偏移量為負數,則它將從字串末尾開始計數。

  • **$encoding−** 如果 $encoding 引數不存在或為 null,則字串將假定它可能以 **iconv.internal_encoding** 編碼。

返回值

**iconv_strpos()** 函式返回 $needle 在 $haystack 中第一次出現時的數字位置。如果未找到 $needle,則該函式將返回 False。

**注意:**從 PHP 8.0 版本開始,編碼是可為空的,從 PHP 7.1 開始,**iconv_strpos()** 函式支援負偏移量。

示例 1

即時演示

<?php
   # UTF-8 string
   $int = iconv_strpos("hello world!", "hello",0, "UTF-8");
   // It will returns the number of character
   var_dump($int);
?>

輸出

int(0)

示例 2

即時演示

<?php
   # UTF-8 string
   $int = iconv_strpos("hello world!", "world",0, "UTF-8");

   // It will returns the number of character
   var_dump($int);
?>

輸出

int(6)

更新於: 2021-08-23

366 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.