Implode array of strings in to paragraphs?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Implode array of strings in to paragraphs?

Post by JAB Creations »

I'm trying to implode an array of strings to output in to paragraphs.

The output I'm trying to generate would look like this...

Code: Select all

<p>piece1</p> <p>piece2</p> <p>piece3</p> <p>piece4</p>
Thanks to Jack's help I was able to strip unnecessary line breaks which is what the first part of the script does. I've seen other members use implode before to construct HTML and MySQL and I figured I could use implode to achieve similar results...I'm just not sure exactly how to go about this. Here is what my current attempt looks like...

Code: Select all

<?php
$pizza  = "piece1\npiece2\n\n\npiece3\npiece4\npiece5 piece6";
$pieces = explode("\n",$pizza);
 
// Remove empty array keys...
foreach($pieces as $key => $value)
{
 if ($value=="") {unset($pieces[$key]);}
}
 
// Create paragraphs with two following line breaks...
foreach($pieces as $key => $value)
{
 $comma_separated = implode("<p>".$pieces."</p>\n\n", $pieces);
}
echo $comma_separated;
?>
What am I doing wrong?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Implode array of strings in to paragraphs?

Post by Eran »

What am I doing wrong?
too many things to count..

you are iterating over an array, and imploding it inside the loop. you keep assigning the same parameter the results of that implode, without concatentation. and last, you are misusing the implode function. did you read the documentation for that one?

what you probably wanted to achieve:

Code: Select all

if( ! empty($pieces) ) {
    echo '<p>' . implode("</p>\n\n<p>",$pieces) . '</p>';
}
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Implode array of strings in to paragraphs?

Post by JAB Creations »

Crazy, I should be thinking of this stuff by now right? :lol:

Ok so I've been messing around with the code and while it kind of works I'm trying to figure out why it's repeating and it's still outputting empty paragraphs.

Code: Select all

$pizza  = "piece1\npiece2\n\n\npiece3 piece4";
$pieces = explode("\n",$pizza);
 
foreach($pieces as $key => $value)
{
 if ($key!="") {echo '<p>' . implode("</p>\n\n<p>",$pieces) . '</p>';}
}
It's outputting...

Code: Select all

<p>piece1</p> <p>piece2</p> <p></p> <p></p> <p>piece3 piece4</p><p>piece1</p> <p>piece2</p> <p></p> <p></p> <p>piece3 piece4</p><p>piece1</p> <p>piece2</p> <p></p> <p></p> <p>piece3 piece4</p>
...and I'm trying to output...

Code: Select all

<p>piece1</p> <p>piece2</p> <p>piece3 piece4</p>
Somehow the implode doesn't seem right...as I mess with it PHP seems to output the array however many times there are keys? :?:

Maybe I could simply create a new array and then explode? :|
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Implode array of strings in to paragraphs?

Post by Eran »

did you read what I wrote? the implode should not be in a loop. it runs for the entire array, that's its purpose...

Code: Select all

$pizza  = "piece1\npiece2\n\n\npiece3 piece4";
$pieces = array_filter(explode("\n",$pizza));
if(!empty($pieces)) {
    echo "<p>" . implode("</p>\n\n<p>",$pieces) . "</p>";
}
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Implode array of strings in to paragraphs?

Post by JAB Creations »

I was working with array_filter earlier though definitely not in the way you implemented, I don't think it works with numeric arrays though?

I did manage to merge your original snippet with Jack's from the other thread...

Code: Select all

$pizza  = "piece1\npiece2\n\n\npiece3 piece4";
$pieces = explode("\n",$pizza);
 
foreach($pieces as $key => $value) {if ($value=="") {unset($pieces[$key]);}}
if (!empty($pieces)) {echo '<p>' . implode("</p>\n\n<p>",$pieces) . '</p>';}
However your last snippet is shorter and works as well. Thank you very much for your excellent reply!
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Implode array of strings in to paragraphs?

Post by McInfo »

If you're more interested in the destination than the journey (using implode()), here are two other solutions.

This execution time of this script is comparable to pytrin's array_filter() solution.

Code: Select all

$pizza  = "piece1\npiece2\n\n\npiece3 piece4";
$count = 0;
do {
    $pizza = str_replace("\n\n", "\n", $pizza, $count);
} while ($count > 0);
echo '<p>', str_replace("\n", "</p>\n\n<p>", $pizza), '</p>';
This is slower than the other solutions, but uses less code.

Code: Select all

$pizza  = "piece1\npiece2\n\n\npiece3 piece4";
echo '<p>', preg_replace('/\n+/', "</p>\n\n<p>", $pizza), '</p>'; // Use \n or \v
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 6:27 pm, edited 1 time in total.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Implode array of strings in to paragraphs?

Post by JAB Creations »

Thank you for posting your solutions McInfo. Unfortunately I'm not the sharpest with the concept to code part of programming and so this ended up oddly becoming one of two threads for the same goal oddly enough. My bad! Here is the other thread...
viewtopic.php?f=1&t=104849

This goal in this and the other thread come after validation which is what I'm working with in this thread...
viewtopic.php?f=1&t=104876
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Implode array of strings in to paragraphs?

Post by JAB Creations »

I've encountered an oddity...the function somehow is either adding or retaining white space! Except for the last array item every other array item has a break before the ending tag of the paragraph element in the final output.

I've modified the code a bit and have tried a few other things for the past couple hours trying to figure out where the phantom line break is coming from though no matter how much I tweak, adjust, remove, add, etc I can't seem to get any output without that line break. What am I missing here? :|

Code: Select all

function bb_4_n2p($pizza)
{
 $pieces = explode("\n",$pizza);
 
 foreach($pieces as $key => $value)
 {
  $value=str_replace(" ", " ", $value);
  $value=str_replace("\n", "", $value);
  $value=str_replace("\r", "", $value);
  if (strlen($value)<2) {unset($pieces[$key]);}
 }
 
 foreach($pieces as $key => $value)
 {
  $bb_q = explode("<blockquote>",$value);
  $bb_c = explode("<code>",$value);
 
  if (count($bb_c)=='1' && count($bb_q)=='1')
  {
   $v2 = explode("\n",$value);
   if (isset($result)) {$result .= '<p>'.$v2[0]."</p>\n\n";}
   else {$result = '<p>'.$v2[0]."</p>\n\n";}
  }
  else
  {
   if (isset($result)) {$result .= $value."\n\n";}
   else {$result = $value."\n\n";}
  }
 }
/*
$count = 0;
do
{
    $pizza = str_replace("\n\n", "\n", $pizza, $count);
} while ($count > 0);
$result = '<p>'.str_replace("\n", "</p>\n\n<p>", $pizza).'</p>';
*/
 //$result = '<p>'.preg_replace('/\n+/', "</p>\n\n<p>", $pizza).'</p>';
 return $result;
}
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Implode array of strings in to paragraphs?

Post by McInfo »

Do you have some example input that demonstrates the problem?

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 6:28 pm, edited 1 time in total.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Implode array of strings in to paragraphs?

Post by JAB Creations »

A textarea with a value like...
line 1

line 2

line 3

line 4

line 5
I'm not passing PHP variables any more (just the $_POST data) though something like this should work...

Code: Select all

$pizza = "line 1\nline 2\nline 3\nline 4\nline 5";
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Implode array of strings in to paragraphs?

Post by McInfo »

This loop does not remove the "\r"s from the $pieces array elements because $value is by-value, not by-reference.

Code: Select all

foreach($pieces as $key => $value)
{
    /* ... */
    $value=str_replace("\r", "", $value);
    /* ... */
}
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 6:28 pm, edited 1 time in total.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Implode array of strings in to paragraphs?

Post by JAB Creations »

Are you saying that updating the $value variable is not updating the array element?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Implode array of strings in to paragraphs?

Post by McInfo »

That is correct.

You would have to either use an ampersand before $value so it is passed by reference

Code: Select all

foreach ($pieces as &$value) {
    $value = '';
}
or use the $pieces array within the loop.

Code: Select all

foreach ($pieces as $key => $value) {
    $pieces[$key] = '';
}
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 6:29 pm, edited 1 time in total.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Implode array of strings in to paragraphs?

Post by JAB Creations »

Aw-ha-ha-ha-some!

Thanks a ton! I see it listed on php.net now though it was obviously not something that struck me when I was facing the problem on my own. I'm so going to write some tutorials on my site about this kind of stuff eventually! Thanks again! :)
Post Reply