Home >Backend Development >PHP Problem >How to delete symbols in php regular code

How to delete symbols in php regular code

藏色散人
藏色散人Original
2020-11-04 11:35:002809browse

php正则删除符号的实现方法:首先创建一个PHP示例文件;然后通过正则表达式“preg_replace($pattern, ' ', $str);”删除字符串中的中英文标点符号即可。

How to delete symbols in php regular code

推荐:《PHP视频教程

php正则,删除字符串中的中英文标点符号

原理很简单,正则查找字符串,然后替换

 

英文标点符号,正则中有专用的模式来匹配。中文则需要一一列举

 

代码:

<?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 delete symbols in php regular code. 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