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

How to remove punctuation marks in php

PHPz
Release: 2023-03-05 21:56:02
Original
5223 people have browsed it

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

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!

Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template