Home >Backend Development >PHP Problem >How to remove punctuation marks in php

How to remove punctuation marks in php

PHPz
PHPzOriginal
2020-09-25 14:04:505179browse

php method to remove punctuation marks: first create a PHP sample file; then use the regular expression "preg_replace($pattern, ' ', $str);" to delete Chinese and English punctuation marks in the string .

How to remove punctuation marks in php

Recommended: "PHP Video Tutorial"

php regular, delete Chinese and English punctuation marks in the string

The principle is very simple. Regular expressions search for strings and then replace

English punctuation marks. There are special patterns in the regular expressions to match. For Chinese, you need to list the

codes one by one:

<?php  
$str = "!@#$%^&*(中&#39;文:;﹑•中&#39;文中&#39;文().,<>|[]&#39;\""; 
  
//中文标点
$char = "。、!?:;﹑•"…‘’“”〝〞∕¦‖— 〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔!¡?¿﹖﹌﹏﹋'´ˊˋ―﹫︳︴¯_ ̄﹢﹦﹤‐­˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼()";
  
$pattern = array(
    "/[[:punct:]]/i", //英文标点符号
    &#39;/[&#39;.$char.&#39;]/u&#39;, //中文标点符号
    &#39;/[ ]{2,}/&#39;
);
$str = preg_replace($pattern, &#39; &#39;, $str);
echo $str;

The above is the detailed content of How to remove 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