Open In App

PHP mb_scrub() Function

Last Updated : 01 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The mb_scrub() is an inbuilt PHP function that replaces an invalid character set to the substitute character. If the encoding is not provided. It will use default encoding.

Syntax:

mb_scrub($string, $encoding );

Parameters: This function accepts two parameters that are described below.

  • $string: The input string that you want to scrub, replaces invalid or unmatched surrogate sequences.
  • $encoding: This is the optional parameter. It defines the encoding for replacing the characters set. If encoding is not defined. It will use default encoding.

Return value: The mb_scrub() function returns the string which is replaced with an invalid byte sequence

Program 1: The following program demonstrates the mb_scrub() function.  In the below program. The string is already valid so the string returned as it is.

PHP




<?php
    
$inputString = "Hello World!";
$output = mb_scrub($inputString, "UTF-8");
echo $output;
  
?>


Output

Hello World!

Program 2: The following program demonstrates the mb_scrub() function.In the below program. The string is not valid so this function changes the string according to the default encoding.

PHP




<?php
$inputString = "H\xC3\xA9llo W\xf2rld!";
$scrubbedString = mb_scrub($inputString);
echo $scrubbedString;
?>


Output

Héllo W?rld!

Reference: https://www.php.net/manual/en/function.mb-scrub.php


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads