详解PHP中mb_strpos的使用
mb_strpos
(PHP4>=4.0.6,PHP5,PHP7)
mb_strpos—Findpositionoffirstoccurrenceofstringinastring
mb_strpos—查找字符串在另一个字符串中首次出现的位置
Description
intmb_strpos( string$haystack, string$needle[, int$offset=0[, string$encoding=mb_internal_encoding()]] ) //Findspositionofthefirstoccurrenceofastringinastring. //查找string在一个string中首次出现的位置。 //Performsamulti-bytesafestrpos()operationbasedonnumberofcharacters.Thefirstcharacter'spositionis0,thesecondcharacterpositionis1,andsoon. //基于字符数执行一个多字节安全的strpos()操作。第一个字符的位置是0,第二个字符的位置是1,以此类推。
Parameters
haystack
- Thestringbeingchecked.
- 要被检查的string。
needle
- Thestringtofindinhaystack.Incontrastwithstrpos(),numericvaluesarenotappliedastheordinalvalueofacharacter.
- 在haystack中查找这个字符串。和strpos()不同的是,数字的值不会被当做字符的顺序值。
offset
- Thesearchoffset.Ifitisnotspecified,0isused.Anegativeoffsetcountsfromtheendofthestring.
- 搜索位置的偏移。如果没有提供该参数,将会使用0。负数的offset会从字符串尾部开始统计。
encoding
- Theencodingparameteristhecharacterencoding.Ifitisomitted,theinternalcharacterencodingvaluewillbeused.
- encoding参数为字符编码。如果省略,则使用内部字符编码。
ReturnValues
- Returnsthenumericpositionofthefirstoccurrenceofneedleinthehaystackstring.Ifneedleisnotfound,itreturnsFALSE.
- 返回string的haystack中needle首次出现位置的数值。如果没有找到needle,它将返回FALSE。
Example
mb_strlen($haystack,$encoding)){ break; } } return$haystack; } $replace=mb_str_replace("helloworld!helloworld!helloworld!helloworld!","hello","hi"); echo$replace.PHP_EOL;//hiworld!hiworld!hiworld!hiworld! //hiPHP!hiPHP!hiPHP!hiPHP! echomb_str_replace($replace,"world","PHP").PHP_EOL; echomb_str_replace($replace,"","-").PHP_EOL; //PHP是世界上最好的语言