Jump to content

[SOLVED] exploding after 3 occurances


swamp

Recommended Posts

Hey, I'm exploding $post into paragraphs to then go in divs... My paragraphs are seperated with < br / > tags.

 

           <?php

$spliter  = nl2br($post);
$out = explode("<br />", $spliter);

?>

 

I need it to only explode it after 3 occurances, so first if I had:

 

Hello<br />My name<br />is<br />Swamp<br />I need help<br />with this script  

 

So if i echoed $out[1] it would look like:

 

Hello

My name

is

 

and $out[2] would be:

 

Swamp

I need help

with this script.

 

Cheers!

Link to comment
https://forums.phpfreaks.com/topic/121704-solved-exploding-after-3-occurances/
Share on other sites

try

<?php
$text = 'Hello<br />My name<br />is<br />Swamp<br />I need help<br />with this script';
$text = explode('<br />',$text);
$text =array_chunk($text, 3);
foreach ($text As $k => $v) $text[$k] = implode('<br />', $v);
print_r($text);
?>

thanks, I'm trying to get yours to work sasa, but I'm not too sure how to implement it with my script.

 

This is the whole thing:

 

           <?php
$spliter  = nl2br($post);
$out= explode(".<br />", $spliter);

?>

<?
foreach($out as $k=>$v)

{
//echo '<div id="content_'.$k.'">'.$v

?>

<div id="content_<? echo $k ?>">
<p>
<? echo $v ?>.
    </p>
    <p>
   

<? //if first div then don't put previous button
if ( $k == 0 ) {
echo " ";
} else {
 ?>  <span class="arrow-back"><a href="#" onClick="setVisible('content_<? echo $k -1; ?>');">previous page</a></span>
<? } ?>


  <?  //if last div don't put next button
  $paragraphs = count($out); //count paragraphs
if ( $k ==  $paragraphs -1 ) {
echo " ";
} else {
 ?>  <span class="arrow"><a href="#" onClick="setVisible('content_<? echo $k +1; ?>');">next page</a></span>
<? } ?>

</p>
</div>

<? 

}

?>

If you wanted to do it this way, you could... doesn't deal with arrays.

 

It's very basic, but it accomplishes what you're looking for.

 

<?php
$string='This is the first.<br />This is the second.<br />This is the third.';
$string=str_replace('<br />','</div><div>',$string);
$string.='<div>'.$string;
$string.=$string.'</div>';
?>

If you wanted to do it this way, you could... doesn't deal with arrays.

 

It's very basic, but it accomplishes what you're looking for.

 

<?php
$string='This is the first.<br />This is the second.<br />This is the third.';
$string=str_replace('<br />','</div><div>',$string);
$string.='<div>'.$string;
$string.=$string.'</div>';
?>

 

... no it doesn't. they're trying to group every three br-delimited sections together. this code simply switches br for <div> containers.

Yeah, sorry Jabop but thats not the thing I'm looking for.

 

Has anyone got any further advice on this? It would be very much appreciated

 

sasa's code will do what you're looking for:

 

<?php
$text = 'Hello<br />My name<br />is<br />Swamp<br />I need help<br />with this script';
$text = explode('<br />',$text);
$text =array_chunk($text, 3);
foreach ($text As $k => $v) $text[$k] = implode('<br />', $v);

foreach ($text AS $k => $chunk)
{
  echo '<div id="content_'.$k.'">'.$chunk.'</div>';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.