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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

php word wrap - limiting word size

  • 04-06-2007 3: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,428 ✭✭✭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,570 ✭✭✭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