need help with filtering out big words from string

Discussion in 'PHP' started by bookatechie, Oct 16, 2006.

  1. #1
    Hello all,

    My website pulls entries from a forum and shows them on the entry page but sometimes the post has a word in it that is over 200 characters. The problem is that IE doesnt split the string, it shows it as one line there by messing up my layout and making the entire page look bad.
    I need a way of filtering out these long words from a string and replacing them with "" or split after each section of 50 or what ever else you guys suggest.

    Thanks for the help!
     
    bookatechie, Oct 16, 2006 IP
  2. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
  3. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    $raw_snippet = "what you got from the database";
    $words_array = explode(" ", $raw_snippet);
    $safe_snippet = "";
    foreach ($words_array as $word) {
    if (strlen($word < 200)) {
    $safe_snippet .= $word;
    } else {
    $safe_snippet .= substr($word, 0, 200); //discard the rest or just split it up and add a space etc.
    }
    }
    echo $safe_snippet;
    
    PHP:
    You'll have to check I implemented the fucntion arguments in the correct order.

    :rolleyes: if only I had known that existed...
     
    T0PS3O, Oct 16, 2006 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    Wow

    Things like that just pass us by

    Never knew such hot function existed

    Peace,
     
    Barti1987, Oct 16, 2006 IP
  5. bookatechie

    bookatechie Peon

    Messages:
    225
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    YAY, you guys are great! Thanks a lot, this is exactly what I was looking for.
     
    bookatechie, Oct 16, 2006 IP
  6. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The rule of php: there's already a function that does that.

    90% of the time it's true. :)
     
    jnestor, Oct 16, 2006 IP