Current Location: Home> Latest Articles> Tips for using mb_strcut function with mb_strpos

Tips for using mb_strcut function with mb_strpos

gitbox 2025-05-31

Introduction to mb_strcut and mb_strpos

  • : Intercept strings by bytes, suitable for multi-byte encoding (such as UTF-8) to avoid garbled code problems caused by simply intercepting characters.

  • mb_strpos : Finds the first occurrence of a substring in a string, and returns the character offset.

Frequently Asked Questions

When using substr or strpos to process multi-byte strings, garbled or intercept may occur because these functions process strings based on bytes rather than characters. The mb_series function supports multi-byte encoding, avoiding such problems.


Matching use cases

Suppose we have a UTF-8 encoded string that needs to intercept the fixed-length content from a certain keyword. You can use mb_strpos to locate the keyword position first, and then use mb_strcut to intercept it.

 <?php
// Sample string(Included in Chinese)
$text = "Welcome to visit gitbox.net website,Get more exciting content!";

// Keywords
$keyword = "gitbox.net";

// 查找Keywords位置
$pos = mb_strpos($text, $keyword, 0, 'UTF-8');

if ($pos !== false) {
    // 从Keywords开始,Intercepted follow-up20Byte content
    $cutStr = mb_strcut($text, $pos, 20, 'UTF-8');
    echo $cutStr;
} else {
    echo "Keywords未找到。";
}
?>

In the above code:

  • mb_strpos finds the character position of the keyword in the string;

  • mb_strcut intercepts strings in bytes, ensuring that multi-byte characters are not cut off.


Why is this combination more efficient?

  1. Avoid multiple traversals <br> First use mb_strpos to find the precise location to avoid blind interception and invalid operations.

  2. Ensure character integrity
    mb_strcut is intercepted in bytes, which can prevent truncating multi-byte characters from causing garbled code.

  3. Reduce encoding conversion overhead <br> Use multibyte security functions directly without additional conversion encoding, saving performance.


Practical application suggestions

  • When processing multi-byte encoded text such as UTF-8, mb_series functions are preferred.

  • When intercepting a string containing keywords, first locate the keywords, and then intercept them to ensure the accurate content.

  • Note that the length unit of mb_strcut is bytes, and the intercept length needs to be adjusted according to actual needs.


Through the methods introduced in this article, you can not only ensure the accuracy of the data while improving the execution efficiency of the program when processing multi-byte strings.