Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php word wrap - limiting word size

Options
  • 04-06-2007 4:02pm
    #1
    Registered Users Posts: 648 ✭✭✭


    hi

    i need to cut long word submitted to a forum im developing

    im using

    <? echo wordwrap($group->desc, 40, "<br />\n");?>

    which seems to work when the text is normal sentances of text - however if someone submits a big long word like below it is not treated at all.

    11111111111111111111111111111111111111111111111111111111112222222222222222222222222222222222222222223333333333333333333333333333333333334444444444444444444444444444444444444555555555555555555555555555


    anyone know how i could handle this ?

    tnx


Comments

  • Registered Users Posts: 3,401 ✭✭✭randombar


    I'm using the command

    if (strlen($text)>30) $text=substr($text,0,30)." ...";

    Never seemed to have a problem with it?


  • Registered Users Posts: 648 ✭✭✭ChicoMendez


    yes but this get the length of the string - and not seperate words in that string.. i dont want to shorten teh string i just want to ensure that long words or lines of characters are broken up so as not to break the site !


  • Registered Users Posts: 6,511 ✭✭✭daymobrew


    Look at post #9 in the "PHP - Text Length" thread. I used wordwrap and then explode'd the string into an array.
    You could go through each element in the array and further split it if it is longer than the max you want. This further splitting would be quite brute force.


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    there is a function that I often use to show only a certain amount of text from long description:

    [php]
    //truncate memo
    function TruncateMemo($str, $ln){
    if (strlen($str) > 0 && strlen($str) > $ln) {
    $str = str_replace("<br />", chr(10), $str); //replace br tag to space for a clean cut
    $k = 0;
    while ($k >= 0 && $k < strlen($str)){
    $i = strpos($str, " ", $k);
    $j = strpos($str,chr(10), $k);
    if ($i === false && $j === false) { // Not able to truncate
    return $str;
    } else {
    // Get nearest space or CrLf
    if ($i > 0 && $j > 0) {
    if ($i < $j) {
    $k = $i;
    }else{
    $k = $j;
    }
    }elseif ($i > 0) {
    $k = $i;
    }elseif ($j > 0) {
    $k = $j;
    }
    // Get truncated text
    if ($k >= $ln) {
    return substr($str, 0, $k) . "... ";
    } else {
    $k ++;
    }
    }
    }
    } else {
    return $str;
    }
    }
    [/php]


Advertisement