Home > Backend Development > PHP Problem > How to remove utf8 symbols in php

How to remove utf8 symbols in php

藏色散人
Release: 2023-03-17 09:26:01
Original
1814 people have browsed it

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.

How to remove utf8 symbols in php

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 );
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template