PHP tips for using PHP mb_strpos

Source: Internet
Author: User
This article mainly introduces the use of Mb_strpos in PHP, through the use of syntax and examples to give you a detailed analysis of usage, the need for friends reference learning.

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-finding where a string first appears in another string

Description

int Mb_strpos (   string $haystack,   string $needle [,   int $offset = 0 [,   string $encoding = Mb_internal_enc Oding ()]]   )//finds position of the first occurrence of a string in a string.//find the position of string in a string where it appears for the first time. Performs a multi-byte safe Strpos () operation based on number of characters. The first character ' s position is 0, the second character position are 1, and so on.//performs a multi-byte secure Strpos () operation based on the number of characters. The position of the first character is 0, the position of the second character is 1, and so on.

Parameters

Haystack

    • The string being checked.

    • The string to be checked.

Needle

    • The string to find in haystack. In contrast with Strpos (), numeric values is not applied as the ordinal value of a character.

    • Look for this string in haystack. Unlike Strpos (), the value of a number is not treated as a sequential value of the character.

Offset

    • The search offset. If It isn't specified, 0 is used. A negative offset counts from the end of the string.

    • The offset of the search location. If this parameter is not provided, 0 will be used. Negative offset starts counting from the end of the string.

Encoding

    • The encoding parameter is the character encoding. If It is omitted, the internal character encoding value would be used.

    • The encoding parameter is a character encoding. If omitted, the internal character encoding is used.

Return Values

    • Returns the numeric position of the first occurrence of needle in the haystack string. If needle is not found, it returns FALSE.

    • Returns the value of the first occurrence of needle in the haystack of a string. If needle is not found, it will return FALSE.

Example

<?php/** * Created by Phpstorm. * User:zhangrongxiang * DATE:2018/2/2 * Time: PM 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;//13function mb_str_replace ($haystack, $search, $replace, $offset = 0, $encoding = ' auto ') {$len _sch = Mb_strl  En ($search, $encoding);    $len _rep = Mb_strlen ($replace, $encoding); while ($offset = Mb_strpos ($haystack, $search, $offset, $encoding))!== false) {$haystack = Mb_substr ($haystac K, 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 "," 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 is the best language in the world.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.