Azu Posted May 18, 2007 Share Posted May 18, 2007 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 >_> Quote Link to comment Share on other sites More sharing options...
s0c0 Posted May 19, 2007 Share Posted May 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
md_dev Posted May 19, 2007 Share Posted May 19, 2007 hi, have a look at this function str_repeat, it should get you started. cheers, Quote Link to comment Share on other sites More sharing options...
Azu Posted May 19, 2007 Author Share Posted May 19, 2007 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 Quote Link to comment Share on other sites More sharing options...
Azu Posted May 20, 2007 Author Share Posted May 20, 2007 Sorry for bump but I really don't understand what I am doing wrong or how to fix it :< please help.. Quote Link to comment Share on other sites More sharing options...
Azu Posted May 23, 2007 Author Share Posted May 23, 2007 Please can somebody tell me what I am doing wrong that is causing this regex to fail? :s Quote Link to comment Share on other sites More sharing options...
per1os Posted May 23, 2007 Share Posted May 23, 2007 I would take this to the regex forum, as they are the experts. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.