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 >_>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.