Jump to content

php seperate paragraphs into divs


swamp

Recommended Posts

Hey,

 

My script at the moment is this:

 

<?php

$str = $post;

echo nl2br(chunk_split($str,900," </div><div id='content_2'><p>"));

?>

 

Which splits the $post into chunks of 900 characters, after 900 characters it puts it in div 'content_2" - this is good if my $post is under 1800 characters, but not so good if it is more than this. How would I go about putting the next 900 characters in content_3, the 900 after that in content_4 etc etc...

 

Any help much appreciated!

Link to comment
https://forums.phpfreaks.com/topic/119680-php-seperate-paragraphs-into-divs/
Share on other sites

maybe not the fastest way of doing this... but...

 

<?
$i=1;
for($t=0; $t<strlen($string);$t++){
if($t%900==0) $i++;
$out[$i].=$string{$t};
}
?>

 

which will break the string up into groups of 900 chars... then you simply foreach the $out to insert the div's :-)

 

<?
foreach($out as $k=>$v) $out[$k]='<div id="content_'.$k.'">'.$v.'</div>';
$out=implode('',$out);
?>

 

and presto... $out now contains your text, in groups of 900 chars, seperated into <div>s that are sequentially numbered ;D

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.