Current Location: Home> Latest Articles> Practical Tips and Methods for Removing Specific Parts from a String Using mb_strstr Function

Practical Tips and Methods for Removing Specific Parts from a String Using mb_strstr Function

gitbox 2025-08-26

In PHP, when working with strings, we often need to remove specific parts from a string. The mb_strstr() function is a multibyte string function that is particularly useful for handling non-ASCII characters. This article will explain how to use mb_strstr() to remove a designated part from a string.

1. Introduction to mb_strstr() Function

mb_strstr() is a multibyte string function similar to the standard strstr() function, but it is designed specifically for multibyte character sets (such as UTF-8, Shift-JIS, etc.). Therefore, when we need to work with strings containing non-English characters, mb_strstr() is more stable and reliable.

Syntax:

<span><span><span class="hljs-title function_ invoke__">mb_strstr</span></span><span>(</span><span><span class="hljs-keyword">string</span></span> <span>$haystack</span>, <span class="hljs-keyword">string</span> <span>$needle</span>, <span class="hljs-keyword">bool</span> <span>$before_needle</span> = <span class="hljs-literal">false</span>, <span class="hljs-keyword">string</span> <span>$encoding</span> = <span class="hljs-literal">null</span>): <span class="hljs-keyword">string</span>|<span class="hljs-literal">false</span>
  • $haystack: The target string to search within.

  • $needle: The substring to look for.

  • $before_needle (optional): If set to true, returns the part before needle; otherwise, returns needle and the part after it.

  • $encoding (optional): Specifies the character encoding, defaulting to mb_internal_encoding().

2. Removing a Specific Part from a String

Suppose we have a string and want to remove a part of it. We can use mb_strstr() to locate that part and then remove it through string manipulation.

Example: Removing a Specific Substring from a String

Suppose we have the following string:

<span>$str = "I love learning PHP programming"</span>

We want to remove the part "learning". We can first use mb_strstr() to find its position and then process accordingly.

Code Implementation:

<span><?php
$str = "I love learning PHP programming";
$needle = "learning";
<p>// Use mb_strstr to find the position of "learning"<br>
$position = mb_strstr($str, $needle);</p>
<p>// If "learning" is found, remove it<br>
if ($position !== false) {<br>
// Remove the "learning" part from the string<br>
$result = mb_substr($str, 0, mb_strpos($str, $needle)) . mb_substr($str, mb_strpos($str, $needle) + mb_strlen($needle));<br>
echo $result;<br>
} else {<br>
echo "Specified part not found";<br>
}<br>
?><br>

Result:

<span>I love PHP programming

3. Removing the Part After a Substring Using mb_strstr()

If we only want to remove the part after a substring, we can use the before_needle parameter of mb_strstr().

Example: Remove the Part After a Substring

<?php
$str = "I love learning PHP programming";
$needle = "learning";
<p>// Get the part before "learning"<br>
$before = mb_strstr($str, $needle, true);</p>
<p>echo $before;<br>
?><br>

Result:

<span>I love

4. Example: Removing Multiple Substrings

If we need to remove multiple different substrings from a string, we can combine mb_strstr() with other string manipulation functions. This can be efficiently done using loops and regular expressions.

Example: Removing Multiple Substrings

<?php
$str = "I love learning PHP programming and Web development";
$needles = ["learning", "Web"];
<p>// Remove multiple substrings<br>
foreach ($needles as $needle) {<br>
$str = str_replace($needle, "", $str);<br>
}</p>
<p>echo $str;<br>
?><br>

Result:

<span>I love PHP programming and development

5. Precautions

  • mb_strstr() is optimized for multibyte character sets and generally performs better than the standard strstr() when handling Chinese, Japanese, Korean, and other multibyte characters.

  • When working with multibyte characters, ensure the correct encoding is specified; otherwise, the results may be inaccurate.

  • In some cases, combining mb_substr() and mb_strpos() allows precise control over which parts of a string are removed.

Conclusion

Using the mb_strstr() function, we can easily remove specific parts from a string, especially when working with multibyte character sets. Mastering these techniques not only improves efficiency in string manipulation but also helps avoid common encoding issues.