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
Share on other sites

          <?php

 

$spliter  = nl2br($post);

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

 

 

 

$first=$out[0]."br ".$out[1]."br ".$out[2]."br ".;

$second=$out[3]."br ".$out[4]."br ".$out[5]."br ".;

 

?>

 

i think is working the same way as you need

Link to comment
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);
?>

Link to comment
Share on other sites

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>

<? 

}

?>

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
}
?>

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.