佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > 詳解PHP中mb_strpos的使用

詳解PHP中mb_strpos的使用

熱門(mén)標(biāo)簽:銀行業(yè)務(wù) 阿里云 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 團(tuán)購(gòu)網(wǎng)站 Linux服務(wù)器 電子圍欄 Mysql連接數(shù)設(shè)置 服務(wù)器配置

mb_strpos

(PHP 4 >= 4.0.6, PHP 5, PHP 7)
mb_strpos — Find position of first occurrence of string in a string
mb_strpos — 查找字符串在另一個(gè)字符串中首次出現(xiàn)的位置

Description

int mb_strpos ( 
  string $haystack , 
  string $needle [, 
  int $offset = 0 [, 
  string $encoding = mb_internal_encoding() ]] 
  )
//Finds position of the first occurrence of a string in a string.
// 查找 string 在一個(gè) string 中首次出現(xiàn)的位置。

//Performs a multi-byte safe strpos() operation based on number of characters. The first character's position is 0, the second character position is 1, and so on.
// 基于字符數(shù)執(zhí)行一個(gè)多字節(jié)安全的 strpos() 操作。 第一個(gè)字符的位置是 0,第二個(gè)字符的位置是 1,以此類推。

Parameters

haystack

  • The string being checked.
  • 要被檢查的 string。

needle

  • The string to find in haystack. In contrast with strpos(), numeric values are not applied as the ordinal value of a character.
  • 在 haystack 中查找這個(gè)字符串。 和 strpos() 不同的是,數(shù)字的值不會(huì)被當(dāng)做字符的順序值。

offset

  • The search offset. If it is not specified, 0 is used. A negative offset counts from the end of the string.
  • 搜索位置的偏移。如果沒(méi)有提供該參數(shù),將會(huì)使用 0。負(fù)數(shù)的 offset 會(huì)從字符串尾部開(kāi)始統(tǒng)計(jì)。

encoding

  • The encoding parameter is the character encoding. If it is omitted, the internal character encoding value will be used.
  • encoding 參數(shù)為字符編碼。如果省略,則使用內(nèi)部字符編碼。

Return Values

  • Returns the numeric position of the first occurrence of needle in the haystack string. If needle is not found, it returns FALSE.
  • 返回 string 的 haystack 中 needle 首次出現(xiàn)位置的數(shù)值。 如果沒(méi)有找到 needle,它將返回 FALSE。

Example

?php
/**
 * Created by PhpStorm.
 * User: zhangrongxiang
 * Date: 2018/2/2
 * Time: 下午11:16
 */

$str = "Hello World! Hello PHP";
$pos = mb_strpos( $str, "Hello", 0, mb_internal_encoding() );
echo $pos . PHP_EOL;//0
$pos = mb_strpos( $str, "Hello", 2, mb_internal_encoding() );
echo $pos . PHP_EOL;//13

function mb_str_replace( $haystack, $search, $replace, $offset = 0, $encoding = 'auto' ) {
  $len_sch = mb_strlen( $search, $encoding );
  $len_rep = mb_strlen( $replace, $encoding );
  
  while ( ( $offset = mb_strpos( $haystack, $search, $offset, $encoding ) ) !== false ) {
    $haystack = mb_substr( $haystack, 0, $offset, $encoding )
          . $replace
          . mb_substr( $haystack, $offset + $len_sch,
        $le = mb_strlen( $haystack ) - mb_strlen( $search ) + mb_strlen( $replace ),
        $encoding );
    //echo $le.PHP_EOL;
    $offset = $offset + $len_rep;
    if ( $offset > mb_strlen( $haystack, $encoding ) ) {
      break;
    }
  }
  
  return $haystack;
}

$replace = mb_str_replace( "hello world !hello world !hello world !hello world !", "hello", "hi" );
echo $replace . PHP_EOL; //hi world !hi world !hi world !hi world !

//hi PHP !hi PHP !hi PHP !hi PHP !
echo mb_str_replace( $replace, "world", "PHP" ) . PHP_EOL;
echo mb_str_replace( $replace, " ", "-" ) . PHP_EOL;

//PHP是世界上最好的語(yǔ)言😂😂😂😂😂😂
echo mb_str_replace( "PHP是世界上最好的語(yǔ)言😄😄😄😄😄😄", '😄', '😂', 0, mb_internal_encoding() ) . PHP_EOL;
echo mb_str_replace( "112233445566", '22', '00' ) . PHP_EOL;//110033445566
echo mb_str_replace( '😄😄😄😄', '😄', '😫1', 2, mb_internal_encoding() ) . PHP_EOL;
echo mb_str_replace( '1111', '111', '0', 1 ) . PHP_EOL;//10
echo mb_strlen( '😄😄😄😄' ) . PHP_EOL;//4

//代碼開(kāi)發(fā)代碼
echo mb_str_replace( '軟件開(kāi)發(fā)軟件', '軟件', '代碼' ,0,mb_internal_encoding()) . PHP_EOL;
//代碼開(kāi)發(fā) //todo??
echo mb_str_replace( '軟件開(kāi)發(fā)軟件', '軟件', '代碼' ) . PHP_EOL;

標(biāo)簽:衡水 蚌埠 萍鄉(xiāng) 棗莊 大理 衢州 廣元 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解PHP中mb_strpos的使用》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    黄石市| 商南县| 丰台区| 明水县| 唐海县| 军事| 武义县| 盈江县| 枣庄市| 渑池县| 象山县| 高台县| 镇雄县| 东阳市| 济阳县| 贵南县| 石楼县| 开鲁县| 朝阳县| 泽州县| 临安市| 广东省| 汝南县| 邵阳县| 余姚市| 灯塔市| 永昌县| 阳泉市| 阿瓦提县| 南溪县| 建宁县| 康保县| 定结县| 平阴县| 温州市| 德阳市| 迁西县| 张家口市| 巨野县| 息烽县| 开阳县|