Home > Backend Development > PHP Tutorial > PHP uses regular expressions to remove duplicate elements from strings

PHP uses regular expressions to remove duplicate elements from strings

PHP中文网
Release: 2023-02-28 21:00:01
Original
1816 people have browsed it

php利用正则表达式删除字符串里的重复元素:

编写代码如下:

<?php
  $str="我...我要要要...学学学..编编程";
  $str=preg_replace(&#39;/\./&#39;,&#39;&#39;,$str);
  $str=preg_replace(&#39;/(.)\1+/u&#39;,&#39;$1&#39;,$str);
  echo $str;
Copy after login

输出的结果就是:

我要学编程
Copy after login

以上就介绍了 php利用正则表达式删除字符串里的重复元素,包括了方面的内容,更多相关内容请关注PHP中文网(www.php.cn)!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template