Split large text

Discussion in 'PHP' started by Kyriakos, Feb 28, 2011.

  1. #1
    Hi,

    I have a website in PHP and mySQL. I'm writting some articles with large text. How i can split automatically the text by 8000 characters per field. Now i have 3 textareas and i'm splitting the text manually.

    This is my query:
    $sql="INSERT INTO articles (title, cat, descr, text1, text2, text3, status, mydate)
    VALUES ('$_POST[title]', '$_POST[cat]', '$_POST[descr]', '$_POST[text1]', '$_POST[text2]', '$_POST[text3]', '$_POST[status]', '$mydate')" OR die(mysql_error());
    
    mysql_query($sql,$con);
    PHP:
    Is there a way that i can do it?

    Thanks in advance
     
    Kyriakos, Feb 28, 2011 IP
  2. webcodez

    webcodez Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi there,

    I'm not sure if I understood your question correctly, but if this is what you want to do: have one textfield and split the contents of it into 3 seperate texts ( each of max 8000 characters ). If that's the case, something like this should do the job:

    
    $text = "some very long text";
    $text1 = substr($text, 0, 8000);
    $text2 = substr($text, 8000, 16000);
    $text3 = substr($text, 16000, 24000);
    
    PHP:
    or for 'endless' split-ups (in general) it'd be something like this I suppose;

    
    $text = "some very long text";
    $texts = array(); //split-ups
    $start = 0;
    $increase = 8000;
    while($start < strlen($text)) {
      if( ($start + $increase) > strlen($text)) {
         $limit = strlen($text);
      }else{
         $limit = $start + $increase;
      }
      $texts[] = substr($text, $start, $limit);
      $start += $increase;
    }
    
    PHP:
    but I'd still be curious why you'd want to do this ( if I understood you correctly ).

    Let me know if I misunderstood or if you have any further questions,

    Cheers.
     
    Last edited: Feb 28, 2011
    webcodez, Feb 28, 2011 IP
  3. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    I want to use one textarea only. Now i'm using 3 textareas to split the text manually if it's more than 8000 characters. I think that the first example is ok. Thank you.
     
    Kyriakos, Feb 28, 2011 IP
  4. webcodez

    webcodez Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Alright, let me know if you need any further help :).
     
    webcodez, Mar 1, 2011 IP
  5. albertpalacci

    albertpalacci Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Why don't you store the data into one field?
     
    albertpalacci, Mar 1, 2011 IP