Current Location: Home> Function Categories> mb_scrub

mb_scrub

Replace malformed byte sequences with substitute characters
Name:mb_scrub
Category:Multi-byte string
Programming Language:php
One-line Description:Clean up illegal characters in strings and convert them into legal character encodings

Function name: mb_scrub()

Applicable version: PHP 8.0.0+

Usage: The mb_scrub() function is used to clean up illegal characters in a string and convert them into legal character encoding. It can solve some character encoding problems, especially when processing user input or receiving data from an external source.

Syntax: mb_scrub(string $str [, string $enc = mb_internal_encoding()]): string|false

parameter:

  • $str: The string to clean.
  • $enc: Optional parameter, specifying the character encoding to use. If not specified, internal character encoding is used by default.

Return value:

  • If the string is cleaned successfully, the cleaned string is returned.
  • If the string cannot be cleaned, false is returned.

Example:

 $str = "Héll?, W?rld!"; $cleanedStr = mb_scrub($str); echo $cleanedStr; // 输出:Héll?, W?rld! $invalidStr = "Héll? \x80 W?rld!"; $cleanedStr = mb_scrub($invalidStr); echo $cleanedStr; // 输出:Héll? ? W?rld!

Notes:

  • The mb_scrub() function is only available in PHP 8.0.0 and later.
  • If the specified character encoding is invalid or not supported, the function will clean up using internal character encoding.
  • If the entire string cannot be cleaned, the function will clean up part of the string as much as possible and return part of the cleaned string.
  • The mb_scrub() function does not modify the original string, but returns a new cleaned string.
Similar Functions
Popular Articles