Results 1 to 4 of 4

Thread: [RESOLVED] Detecting repeating strings

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2004
    Location
    South Carolina
    Posts
    19

    Resolved [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.
    Last edited by Distortion; Jun 25th, 2006 at 11:48 PM.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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'); 

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2004
    Location
    South Carolina
    Posts
    19

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width