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

How to delete symbols in php regular code

藏色散人
Release: 2023-03-07 08:48:02
Original
2808 people have browsed it

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

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!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template