Jump to content

Wrap word lenth problem


firestarter30

Recommended Posts

Hello all 1st post here :)

 

I red in an older post how to add <br /> using the wordwrap() function , ok i used it it worked , but not as i was expecting and here is the point i need your help.

 

I have a table which i pull some strings from the database.Horizontal space is limited to i wanted arround 70 charecters per line.

The problem i have is because i used the above function the text ofcourse always keeps 70 characters and makes the text formation not to look so appealing..here is what i mean as an example

 

Pros: bla bla bla bla bla bla  bla bla bla bla bla bla

bla bla bla bla bla bla bla bla bla bla bla bla

bla bla bla bla bla bla bla bla bla bla bla bla

 

This is how the output is looks like in the page. I would like something like this :

 

Pros: bla bla bla bla bla bla bla bla bla bla bla blabla blabla bla bla bla bla

bla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla bla

bla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla bla

 

If there is a way to tell php : hey make the 1st line to have 70 characters and all the rest to have 90.

 

Is a visual problem in other words.

The code is this Pros: <?php echo wordwrap(($row_site_list['pros']),70,"<br />",true)?><br />

Any ideas, pointers?

 

Thanks :)

Link to comment
Share on other sites

Hmmm i think it was from the beginning my mistake cause after i insert to the css a padding 5px left, and 5px right, the problem solved without any wordwrap php :wtf:

 

One other thing i would like to ask.

Is there a way after a certain character length, lets say 100 ,  to output " ...more " so i can link "...more " to a details page?

 

Thanks again for your time :)

Link to comment
Share on other sites

Here's a simple function that will truncate a string if it is over the specified number of characters (and add ellipses, or whatever characters you wish):

 

function truncateString($string, $length, $ellipse='...')
{
    if (strlen($string) <= $length) { return $string; }
    return array_shift(explode("\n", wordwrap($string, $length))) . $ellipse;
}

$text = "This is a long string with many, many words and it should be truncated";

echo truncateString($text, 45);
//Output: "This is a long string with many, many words..."

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.