Open In App

PHP mb_scrub() Function

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.

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
    
$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
$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

Article Tags :