views:

108

answers:

6

Using explode I broke up the text into pieces, then us the foreach to look for a few thing in the text.

$pieces = explode(' ', $text);

foreach ($pieces as $piece) {
    Some Modification of the piece
}

My questions so how can I put those pieces back together? So I can wordwrap the text. Some like this:

piece 1 + piece 2 + etc
+2  A: 

You would use the implode() function.

http://php.net/manual/en/function.implode.php

string implode ( string $glue , array $pieces )
string implode ( array $pieces )

EDIT: Possibly the most misleading question ever.

If you're trying to word wrap the images you're constructing, perhaps you could put them all in individual div's in order with the float:left style.

Fosco
but implode only works on a string and piece isn't a string.
timg
but explode only works on a string, creating an array of strings... So what have you done to the string? That would be a good piece of information to put in your question.
Fosco
I modified my question showing what I have done to the string. Basically breaks the text looks for a url, colors the url a different color and then displays as an image. Why I need to try to put the piece back together is for wordwrap. If there is an url that is to long the url doesn't wrap to the next line.
timg
Sorry that my question is misleading, just trying to get helpI need to only create one image, so creating multiple image isn't possible.
timg
+2  A: 

First of all, if you want to modify each $piece in the loop inline, you have to loop over the items as references:

foreach ($pieces as &$piece)

When the loop is finished, you can produce a single string again by using join():

$string = join(' ', $pieces);

(The first parameter to join() is the separator that glues the pieces together. Use whatever fits your application best.)

igor
afair join() is deprecated and should be replaced by implode (which has the same syntax, i think)
dbemerlin
naw. join()'s an alias of implode() and isn't deprecated.
Marc B
This is the most concise and helpful answer.
hopeseekr
A: 

$pieces = implode(' ', $pieces);

hugo_leonardo
FYI: implode is the same as join.
hopeseekr
but i preffer implode since the opposite function is called 'explode'. easier to remember (:
hugo_leonardo
A: 

Question is quite confusing, but would something like this work:

$new = implode(' ', $pieces);
echo wordwrap($new, 120); // wordwrap 120 chars
Kieran Allen
+1  A: 

All of the answers so far make it much more difficult than it is. Why not put it back together as you modify it? I think this is what you are looking for.

$pieces = explode(' ', $text);

// text has already been passed to pieces so unset it
unset($text);

foreach ($pieces as $piece) {
    Some Modification of the piece
    // rebuild the text here
    $text .= {MODIFIED PIECE};
}

// print the new modified version
echo $text;
cdburgess
+1  A: 

Here's my take

$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ornare tincidunt euismod. Pellentesque sodales elementum tortor posuere mollis. Curabitur in sem eu urna commodo vulputate.\nVivamus libero velit, auctor accumsan commodo vel, blandit nec turpis. Sed nec dui sit amet velit interdum tincidunt.";

// Break apart at new lines.
$pieces = explode("\n", $text);

// Use reference to be able to modify each piece.
foreach ($pieces as &$piece)
{
    $piece = wordwrap($piece, 80);
}

// Join the pieces together back into one line.
$wrapped_lines = join(' ', $pieces);

// Convert new lines \n to <br>.
$wrapped_lines = nl2br($wrapped_lines);
echo $wrapped_lines;

/* Output:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ornare tincidunt<br />
euismod. Pellentesque sodales elementum tortor posuere mollis. Curabitur in sem<br />
eu urna commodo vulputate. Vivamus libero velit, auctor accumsan commodo vel, blandit nec  turpis. Sed nec<br />
*/
hopeseekr