PHP – iconv_strpos() 函數 – 在字串中尋找第一個出現的子字串的位置

WBOY
發布: 2023-08-20 18:46:01
轉載
741 人瀏覽過

PHP – iconv_strpos() 函数 – 在字符串中查找第一个出现的子串的位置

在PHP中,iconv_strpos()函數用於從給定的字串中讀取第一個字元。它找到字串中第一個字元的位置。這是PHP中的內建函數。

語法

string iconv_strpos(string $haystack, string $needle, int $offset, string $encoding)
登入後複製

Note: strpos(), the return value of iconv_strpos() is the number of characters that appear before the needle, rather than the offset in bytes to the position where the needle has been found. The characters are counted based on the specified character set encoding.

Parameters

iconv_strpos() function accepts four differparaers paraers#; $haystack, $needle, $offset$encoding.

  • $haystack− It denotes the whole string.

  • #$needle− The $needle parameter is used to search the substring from the given whole string.

  • $offset− The $offset parameter is optional it is used to specify the position from which the search should be performed. If the offset is negative then it will count from the end of the string.

  • $encoding− if the $encoding parameter is absent or null, then the string will assume that it may be encoded in iconv.internal_encoding.

Return Values

The iconv_strpos() function returns the numeric position of the first occurrence of the needle in the haystack. If the needle is not found, then the function will return False.

Note: From PHP 8.0 version, encoding is nullable and from PHP# iconv_strpos() function support for the negative offsets 有 been added.

Example 1

 Live Demo

<?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)
登入後複製

Example 2

 Live Demo

<?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)
登入後複製

以上是PHP – iconv_strpos() 函數 – 在字串中尋找第一個出現的子字串的位置的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!