Skip to content
PHP Logo

How can UTF-8 encoding be used to remove special characters like | in PHP output?

Special characters like | can be removed from PHP output by using UTF-8 encoding and the `preg_replace` function to replace them with an empty string. This can be done by specifying the UTF-8 encoding in the `preg_replace` function and using a regular expression pattern to match the special characters that need to be removed.

php
<?php
// Original PHP string with special characters
$string = "Hello | World!";

// Remove special characters using UTF-8 encoding
$clean_string = preg_replace('/[^\p{L}\p{N}\s]/u', '', $string);

// Output the cleaned string
echo $clean_string;
?>