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中文网
PHP中文网Original
2017-03-30 17:29:101829browse

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

编写代码如下:

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

输出的结果就是:

我要学编程

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

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