Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

php word wrap - limiting word size

  • 04-06-2007 04:02PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 3,484 ✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 6,652 ✭✭✭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