English 中文(简体)
PHP - 需要删除字符串中的重复字符,但需要包括例外
原标题:PHP - Need to remove duplicate characters within a String but would like to include exceptions

我一直在互联网上寻找解决办法,但找不到解决办法。

我需要删除字符串中的重复字符, 但也希望包含一个例外, 允许字符的整数重复 / 保留在字符串中 。

" 强 " 例如,我尝试了以下方法: "/强 "

$str =  This ----------is******** a bbbb 999-999-9999 ******** 8888888888 test 4444444444 ********##########Sammy!!!!!! ###### hello !!!!!! ;

$t1 = preg_replace( /(.)1{3,}/ ,  ,$str);
$t2 = preg_replace( /(S)1{3,}/ ,  ,$str);
$t3 = preg_replace( {(.)1+} , $1 ,$str);
$t4 = preg_replace("/[;,:s]+/", , ,$str);
$t5 = preg_replace( /W/ ,   , $str);
$t6 = preg_replace( "/[^a-z]/i", "", $str);

echo  $t1 =  .$t1. <br> ;
echo  $t2 =  .$t2. <br> ;
echo  $t3 =  .$t3. <br> ;
echo  $t4 =  .$t4. <br> ;
echo  $t5 =  .$t5. <br> ;
echo  $t6 =  .$t6. <br> ;

<强 > 成果:

$t1 = This is a 999-999- test Sammy hello 
$t2 = This is a 999-999- test Sammy hello 
$t3 = This -is* a b 9-9-9 * 8 test 4 *#Samy! # helo !
$t4 = This,----------is********,a,bbbb,999-999-9999,********,8888888888,test,4444444444,********##########Sammy!!!!!!,######,hello,!!!!!!
$t5 = Thisisabbbb99999999998888888888test4444444444Sammyhello
$t6 = ThisisabbbbtestSammyhello

" 强 " 意为: " 强 " / " 强 "

This ---is*** a bbbb 999-999-9999 *** 8888888888 test 4444444444 ***###Sammy!!! ### hello !!!

您可以看到, “ 坚固” 想要的 < / 坚固” 输出会让数字变得孤单, 只留下三个重复字符, 即 - - - @ !!!!! @ label

如果可能,我希望能够将例外从3个改为任何其他整数。

提前感谢。

最佳回答

这将做到:

preg_replace( /(([^d])22)2+/ ,  $1 , $str);

[^d] matches a single character which isn t a digit.
2 refers to the captured digit
$1 refers to the first captured group which will be the first three repeated characters, so the extra 2+ gets stripped off.

"http://codepad.viper-7.com/d3Qyd4" rel="不跟随"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

问题回答

The regex you are looking for: /((.)2{2})2*/ If you need exception n, put n-1 in the curly brace {n-1}: /((.)2{n-1})2*/

EDIT: 对于非编号或什么, 请替换 < code> 。 。 < / code > 改为其它东西, 例如 < code> < {d] 等 。 < code>/ (([{d]) 2{2}) */





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...