php method to remove utf8 symbols: 1. Create a PHP sample file; 2. Use "preg_replace('/[x00-x08x10x0Bx0Cx0E-x19x7F]'." regular method to match illegal utf8 symbols and delete them.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
php How to remove utf8 symbols ?
PHP Delete illegal UTF-8 characters
The code is as follows:
//reject overly long 2 byte sequences, as well as characters above U+10000 and replace with ? $some_string = preg_replace('/[x00-x08x10x0Bx0Cx0E-x19x7F]'. '|[x00-x7F][x80-xBF]+'. '|([xC0xC1]|[xF0-xFF])[x80-xBF]*'. '|[xC2-xDF]((?![x80-xBF])|[x80-xBF]{2,})'. '|[xE0-xEF](([x80-xBF](?![x80-xBF]))|(?![x80-xBF]{2})|[x80-xBF]{3,})/S', '?', $some_string ); //reject overly long 3 byte sequences and UTF-16 surrogates and replace with ? $some_string = preg_replace('/xE0[x80-x9F][x80-xBF]'. '|xED[xA0-xBF][x80-xBF]/S','?', $some_string );
preg_replace function performs a regular expression search and replace.
Syntax
mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit = -1 [, int &$count ]] )
Search for matching pattern in subject part, replace with replacement.
Parameter description:
-
$pattern: The pattern to be searched, which can be a string or a string array.
$replacement: The string or string array used for replacement.
$subject: The target string or string array to search for replacement.
$limit: Optional, the maximum number of substitutions for each subject string per pattern. The default is -1 (no limit).
$count: Optional, the number of times the replacement is performed.
Return value
If subject is an array, preg_replace() returns an array , otherwise a string is returned.
If a match is found, the replaced subject is returned, otherwise the unchanged subject is returned. If an error occurs, NULL is returned.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove utf8 symbols in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
