Jump to content

word wrap a string in an array


svgmx5

Recommended Posts

my issue is the following:

 

I'm retrieving data from my database and putting the result data in an array. I then implode the array so each row of data has a "," at the end of it. The issue is that when i output this data it does not break as it should after it reaches the end of my block instead if keeps on going overlapping other blocks of data on my site.

 

What i want to do and can't figure out is how can i split the data in rows based on a certain number of characters, but at the same time make sure that a word is not broken.

 

So for example an string like :

 

red, white, blue, pink, orange, black.

 

I want this string to add a "<br/>" after it reaches a certain number of characters but make sure that a word is not broken.

 

so i can have something like :

 

red, white, blue, pink

orange, black

 

Instead of this:

 

red, white blue, pink, ora

nge, black

 

I have tried using the CSS attribute of "word-wrap: break-word" but this just breaks the word as the above example.

 

Anyways, i have placed my code below, hope someone can help me out. Thanks in advanced

 

$array = array();
$i = 0;

if($results!=0){
while($row = mysql_fetch_assoc($query)){
	$array[] = $row['name'].' ';
	$i++;
}

echo implode(',',$array);
}

Link to comment
Share on other sites

can't believe it was this simple to do this. Just another question, while this is working fine, i was wondering if there was a way to remove the ',' at the end of the word after it makes the break.

 

Right now the string shows up like:

 

red, black, white,

pink, orange

 

Its not a huge deal but if possible i'd like to be able to remove the last ',' at the end of each line break.

 

Link to comment
Share on other sites

But... it's a list. You don't just remove commas because the list spans multiple lines.

 

There isn't really any good way to do it. You'd have to know in your PHP where the line break is going to be but that depends on fonts and sizes and widths and all manners of things which you can't really know for sure about.

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.