Jump to content

[SOLVED] Detecting if something is repeated 3 times


Recommended Posts

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".

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

 

 

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.

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.";?>

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.";
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.