swamp Posted August 28, 2008 Share Posted August 28, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/121704-solved-exploding-after-3-occurances/ Share on other sites More sharing options...
alin19 Posted August 28, 2008 Share Posted August 28, 2008 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/121704-solved-exploding-after-3-occurances/#findComment-627813 Share on other sites More sharing options...
sasa Posted August 28, 2008 Share Posted August 28, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/121704-solved-exploding-after-3-occurances/#findComment-627817 Share on other sites More sharing options...
tibberous Posted August 28, 2008 Share Posted August 28, 2008 Cheap way is to explode it, shift off the first three if their not null, then join them with <br /> Quote Link to comment https://forums.phpfreaks.com/topic/121704-solved-exploding-after-3-occurances/#findComment-627824 Share on other sites More sharing options...
swamp Posted August 28, 2008 Author Share Posted August 28, 2008 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> <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/121704-solved-exploding-after-3-occurances/#findComment-627838 Share on other sites More sharing options...
Jabop Posted August 28, 2008 Share Posted August 28, 2008 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/121704-solved-exploding-after-3-occurances/#findComment-627843 Share on other sites More sharing options...
akitchin Posted August 28, 2008 Share Posted August 28, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121704-solved-exploding-after-3-occurances/#findComment-627868 Share on other sites More sharing options...
swamp Posted August 28, 2008 Author Share Posted August 28, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/121704-solved-exploding-after-3-occurances/#findComment-627872 Share on other sites More sharing options...
akitchin Posted August 28, 2008 Share Posted August 28, 2008 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/121704-solved-exploding-after-3-occurances/#findComment-627881 Share on other sites More sharing options...
swamp Posted August 28, 2008 Author Share Posted August 28, 2008 Ah yes, so it did - thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/121704-solved-exploding-after-3-occurances/#findComment-627890 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.