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
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

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.