Azu Posted May 25, 2007 Share Posted May 25, 2007 Can somebody please tell me what is wrong with this? $str=preg_replace('/(.){3}/', 'X',$str); It's supposed to only match something if it is repeated 3 times in a row, but instead it matches any sequence of characters that are longer then 2 characters.. =[ How can I make it so that the (.){3} only matches if the . is repeated 3 times in a row? It should match anything as long as it is repeated 3 times in a row hence the {3}. I need it to be so that the . will only change once it has finished checking the one it is on to see if it is there 3 times in a row, only then should the . change to another string.. Please help I'm stumped =S It should find anything that is repeated 3 times and replace it with "X". Link to comment https://forums.phpfreaks.com/topic/52943-solved-detecting-if-something-is-repeated-3-times/ Share on other sites More sharing options...
paul2463 Posted May 25, 2007 Share Posted May 25, 2007 the "." is a special character in regex (sorry if you know this) but should it not be <?php $str=preg_replace('/\.{3}/', 'X',$str); //with the . escaped and looking for 3 of them ?> no where to test it at work so it is an untested guess - sorry Link to comment https://forums.phpfreaks.com/topic/52943-solved-detecting-if-something-is-repeated-3-times/#findComment-261474 Share on other sites More sharing options...
effigy Posted May 25, 2007 Share Posted May 25, 2007 <pre> <?php $tests = array( 'aaa', 'aab', 'aac', 'aabaac', 'aabbccc', 'xxxyyyzzz', '' ); foreach ($tests as $test) { echo "<b>$test</b><br>"; preg_match_all('/((.)\2\2)/', $test, $matches); print_r($matches); } ?> </pre> Link to comment https://forums.phpfreaks.com/topic/52943-solved-detecting-if-something-is-repeated-3-times/#findComment-261544 Share on other sites More sharing options...
Azu Posted May 26, 2007 Author Share Posted May 26, 2007 Thanks guys.. can you please tell me what I am doing wrong now? :s <?$this_is_how_it_looks_to_begin_with="This is a teeeeest of repeating something over and over and over and over and over and over many tiiiiiimes like so so so so"; $this_is_how_it_should_look_afterwards="This is a teest of repeating something over and over and over many tiimes like so so"; $str=$this_is_how_it_looks_to_begin_with; while($str!=$old){$old=$str; $str=preg_replace('/((.)\2\2)/','\3',$str);} echo"<style type='text/css'>@import'http://69.89.21.68/~freethep/.css';</style><center><table><tr><td>This is how it looks to begin with</td><td>$this_is_how_it_looks_to_begin_with</td></tr><tr><td>This is how it looks afterwards</td><td>$str</td></tr><tr><td>This is how it SHOULD look afterwords</td><td>$this_is_how_it_should_look_afterwards</td></tr></table>"; echo($str==$this_is_how_it_should_look_afterwards)?"It worked.":"It didn't work.";?> Example: http://tso.hopto.org/test.htm Anything repeated 3 times or more should be shortened to only being repeated 2 times. Link to comment https://forums.phpfreaks.com/topic/52943-solved-detecting-if-something-is-repeated-3-times/#findComment-261882 Share on other sites More sharing options...
Wildbug Posted May 26, 2007 Share Posted May 26, 2007 If you want something that's repeated three times OR MORE, then you need a different regex. preg_replace('/((.)\2{2,})/','$2$2',$str); Link to comment https://forums.phpfreaks.com/topic/52943-solved-detecting-if-something-is-repeated-3-times/#findComment-262039 Share on other sites More sharing options...
Azu Posted May 26, 2007 Author Share Posted May 26, 2007 Thanks, that doesn't work either though <?$this_is_how_it_looks_to_begin_with= "This is a teeeeest of repeating something over and over and over and over and over and over many tiiiiiimes like so so so so"; $this_is_how_it_should_look_afterwards= "This is a teest of repeating something over and over and over many tiimes like so so"; $str=$this_is_how_it_looks_to_begin_with; while($str!=$old){ $old=$str; $str=preg_replace('/((.)\2{2,})/','$2$2',$str);} //REGEX STUFF--------^----------^ echo"<style type='text/css'>@import'http://69.89.21.68/~freethep/.css';</style><center><table><tr><td>This is how it looks to begin with</td><td>$this_is_how_it_looks_to_begin_with</td></tr><tr><td>This is how it looks afterwards</td><td>$str</td></tr><tr><td>This is how it SHOULD look afterwords</td><td>$this_is_how_it_should_look_afterwards</td></tr></table>"; echo($str==$this_is_how_it_should_look_afterwards)?"It worked.":"It didn't work.";?> Link to comment https://forums.phpfreaks.com/topic/52943-solved-detecting-if-something-is-repeated-3-times/#findComment-262296 Share on other sites More sharing options...
dustinnoe Posted May 27, 2007 Share Posted May 27, 2007 <?php $str = "This is a line of text ending with..."; $str=preg_replace('/[\.]{3}/', 'X',$str); echo $str; ?> Try that. Worked for me. Link to comment https://forums.phpfreaks.com/topic/52943-solved-detecting-if-something-is-repeated-3-times/#findComment-262451 Share on other sites More sharing options...
Azu Posted May 27, 2007 Author Share Posted May 27, 2007 Doesn't work Only applies to things that are just a single character long. Link to comment https://forums.phpfreaks.com/topic/52943-solved-detecting-if-something-is-repeated-3-times/#findComment-262479 Share on other sites More sharing options...
MadTechie Posted May 27, 2007 Share Posted May 27, 2007 here this works /((.*)\2{2,})/im <?php $this_is_how_it_looks_to_begin_with= "This is a teeeeest of repeating something over and over and over and over and over and over many tiiiiiimes like so so so so"; $this_is_how_it_should_look_afterwards= "This is a teest of repeating something over and over and over many tiimes like so so"; $str=$this_is_how_it_looks_to_begin_with; while($str!=$old){ $old=$str; $str=preg_replace('/((.*)\2{2,})/im','$2$2',$str);} //REGEX STUFF--------^----------^ echo"<style type='text/css'>@import'http://69.89.21.68/~freethep/.css';</style><center><table><tr><td>This is how it looks to begin with</td><td>$this_is_how_it_looks_to_begin_with</td></tr><tr><td>This is how it looks afterwards</td><td>$str</td></tr><tr><td>This is how it SHOULD look afterwords</td><td>$this_is_how_it_should_look_afterwards</td></tr></table>"; echo($str==$this_is_how_it_should_look_afterwards)?"It worked.":"It didn't work."; ?> Link to comment https://forums.phpfreaks.com/topic/52943-solved-detecting-if-something-is-repeated-3-times/#findComment-262555 Share on other sites More sharing options...
Azu Posted May 27, 2007 Author Share Posted May 27, 2007 Thank you! That works perfectly! Link to comment https://forums.phpfreaks.com/topic/52943-solved-detecting-if-something-is-repeated-3-times/#findComment-262578 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.