How to remove English punctuation marks in PHP
Removal method: Use the preg_replace() function with the regular expression "/[[:punct:]]/i" to find all English punctuation marks in the string and replace them with empty characters. ;Syntax "preg_replace('/[[:punct:]]/i', '', $str)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, you can use the preg_replace() function Use regular expressions to remove English punctuation marks from strings.
Just use the preg_replace() function to replace English punctuation marks with empty strings.
The regular expression used is:
/[[:punct:]]/i
Example: only remove English punctuation marks, Retain other characters (including Chinese punctuation marks)
<?php header("Content-type:text/html;charset=utf-8"); $str = "12!@78#9$%^56&78*9()0.8,7<>8|3[]8'45\"67!【】“45”‘67’"; echo "原字符串:".$str."<br><br>"; $Newstr=preg_replace('/[[:punct:]]/i','',$str); echo "处理后:".$Newstr; ?>
Description
preg_replace() function can perform regular expression search and replace.
preg_replace($pattern, $replacement, $subject [, $limit = -1 [, &$count]])
If $subject is an array, the preg_replace() function will return an array, otherwise it will return a string.
If the function preg_replace() finds a match, it will return the replaced $subject, otherwise it will return the unchanged $subject. Each parameter of the preg_replace() function (except the parameter $limit) can be an array. If the $pattern parameter and the $replacement parameter are both arrays, the function will process the keys in the order they appear in the array. If an error occurs, NULL is returned.
The parameter $replacement can contain back references \\n or $n, the latter is preferred syntactically. Each such reference will be replaced by the text captured by the nth capturing subgroup that was matched. n can be 0-99, with \\0 and $0 representing the complete pattern matching text.
The serial number counting method of capturing subgroups is: the left bracket representing the capturing subgroup is counted from left to right, starting from 1. If you want to use backslashes in $replacement, you must use 4 ("\\\\" because this is first a php string, and after escaping it is two, and then it is considered as a string after passing through the regular expression engine. an original backslash).
When working in replacement mode and the backreference needs to be followed by another number (for example: adding an original number immediately after a matching pattern), you cannot use the syntax \\1. Describes backreferences. For example, \\11 will make preg_replace() unable to understand whether you want a \\1 backreference followed by an original 1, or a \\11 backreference followed by nothing. The solution in this case is to use ${1}1. This creates a separate backreference for $1, a separate backreference for source 1.
When using the deprecated e modifier, this function will escape some characters (ie: ', ", \ and NULL) and then perform backreference replacement. When this is done please make sure to backreference After the reference is parsed, there are no syntax errors caused by single quotes or double quotes (for example: 'strlen(\'$1\') strlen("$2")'). Ensure that it conforms to PHP's string syntax and complies with eval syntax. Because in After completing the replacement, the engine will use the eval method to evaluate the result string as PHP code and use the return value as the final string participating in the replacement.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove English punctuation marks 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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
