Replace Repeated Occurance of Character with 1 of Said Character

Discussion in 'PHP' started by snlildude87, Dec 17, 2006.

  1. #1
    I know the title probably didn't make sense, so I'll clarify what I mean.

    What I'm looking for is a way to cut down repetition of a character (for example, aaaa) down to just 1 of that character (a), so aaaa -> a

    Thank you!
     
    snlildude87, Dec 17, 2006 IP
  2. weknowtheworld

    weknowtheworld Guest

    Messages:
    306
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think you have to use trim function ...
     
    weknowtheworld, Dec 18, 2006 IP
  3. snlildude87

    snlildude87 Member

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    The trim function only works with the beginning and end of the string. I'm looking for something that trims in the middle.
     
    snlildude87, Dec 18, 2006 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Try a preg_replace:

    
    
    $string = preg_replace('/aaaa/','a',$string);
    
    
    PHP:
    Let us know if your $string could contain bbbb, or cccc, or other situations that aren't necessarily pre-identifiable.
     
    jestep, Dec 18, 2006 IP
  5. snlildude87

    snlildude87 Member

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    jestep, I'm only looking for repeated occurrences of one character, but is there a way to cut down repeated occurrences (starting with 2 or more) down to just one?

    So the scenario is, I don't know if there will be 2, 3, 4, or even more As, but if there are, I need to trim that down to just 1.
     
    snlildude87, Dec 18, 2006 IP
  6. wallity

    wallity Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    preg_replace('/[a]+/', 'a', $str);
    PHP:
    Hope that helps
     
    wallity, Dec 18, 2006 IP
  7. snlildude87

    snlildude87 Member

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #7
    Thank you Wallity!!
     
    snlildude87, Dec 18, 2006 IP