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