Jump to content

[SOLVED] Detect phrase being repeated X times in a row?


Azu

Recommended Posts

Hello!

 

Would somebody please tell me if there is a way to find out if something is repeated X times in a row in a string?

 

Like if X is 5 and the string is "Cows go moooooooo" then it would replace the "ooooo" after "m" with Y "o"s, like "oo" if Y=2?

 

Is this possible? And if it is could somebody please help me get started? I have no clue how to do this >_>

Its dooable and if this was tasked to me I could figure it out within a reasonable amount of time, but I'm not going to write the code for you instead look at the following functions via php.net: substr and explode

 

These will help in what our looking to do.  You may also want to look at preg_replace, this is just a matter of using the right functions with the right logic.

Thanks guys!

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.