[RESOLVED] Detecting repeating strings
I'm going to classically preface this by saying that I'm new PHP and, obviously, need some help. I've searched around some and haven't come to any real solution. I'm sure it's probably out there and I just haven't found it yet, but I decided to post here in hopes that someone could just help me.
Like the title says, I'm looking for some guidance when it comes to how to detect repeating strings of text. Incase you're wondering, the strings in question here are dynamic and aren't one specific type.
For example, if I were to, say, type "12121212121212121212" or ":):):):):):):):):):):):):)" I'm looking for a way to detect that there's a large number of repeating characters or sequence of characters and after X amount replace the excess ones with something else. I'm fairly confident that I can replace them, I just need a way to detect the repeating string of characters.
Hopefully that makes sense. Thanks in advance.
Re: Detecting repeating strings
Use preg_replace(). If you are not familiar with Regular Expressions, learn them, they are very useful.
Something like:
PHP Code:
$str = '12121212121212121212';
$str = preg_replace('/(12{1,})/si', '12', $str);
echo $str;
Untested.
If that doesn't work, there's always the loopy way:
PHP Code:
while (strpos($str, '1212') > 0)
$str = str_replace('1212', '12');
Re: Detecting repeating strings
You are best for splitting your string up into an array of words (whitespace separated sequences of characters) and checking for checking for several more occurances of the same element in a row and merging them together.
Your other option would be to traverse the string one character at a time and after a predefined number of characters, check for a duplicate.
Can I ask why you want to do this? There may be an easier way.
Re: Detecting repeating strings
Thanks for the replies, guys.
Quote:
Originally Posted by visualAd
Can I ask why you want to do this? There may be an easier way.
Sure. My main reasoning for wanting to do this is to create a forum mod (for my phpBB forum) that will prevent users from making posts like in the examples of my original post.