Home  >  Article  >  Backend Development  >  How to remove English punctuation marks in PHP

How to remove English punctuation marks in PHP

青灯夜游
青灯夜游Original
2022-05-25 20:24:251825browse

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)".

How to remove English punctuation marks in PHP

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&#39;45\"67!【】“45”‘67’"; 
echo "原字符串:".$str."<br><br>";
$Newstr=preg_replace(&#39;/[[:punct:]]/i&#39;,&#39;&#39;,$str);
echo "处理后:".$Newstr;
?>

How to remove English punctuation marks in PHP

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!

Statement:
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