Skip to content

Instantly share code, notes, and snippets.

@sonicpunk
Created November 14, 2013 13:20

Revisions

  1. sonicpunk created this gist Nov 14, 2013.
    44 changes: 44 additions & 0 deletions contentSplitter
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    <?php
    function splitString($string, $amount)
    {
    $start = 0;
    $end = $amount;

    while ($end < strlen($string)+$amount) //while $end is less than the length of $string + $amount to make sure it gets it all
    {
    $chunk = substr($string, $start, $amount); //assign to variable instead of array
    $chunk = strrev($chunk); // reverse string created
    $new_chunk = preg_split("/[\s]+/", $chunk, 2); // split reversed string into 2 pieces on the first space. So you are left with an array. Item [0] being the part of the last word and item [1] the remaining string

    $page_text = strrev($new_chunk[1]); // reverse the remaining string back so it is readable

    $offset = strlen($new_chunk[0]); // count the length of the part of the word left over so we can substract that from the $end.

    $strArray[] = $page_text; // assign to array

    $start = $end - $offset; // subtract that from the end
    $end = $end + $amount;
    }
    return $strArray;
    }

    $myContent = $modx->resource->getContent();

    $return = splitString($myContent, 1500); //$return = the array of 3600 letter strings

    $num = count($return); // get number of elements in array

    $output = '';

    // loop through the $return array and assign each array element to textchunk so we can print each chunk
    for($x=0;$x<$num;$x++) {

    if ($x==0){
    $output.=$return[$x].'</p>';
    $output.=$modx->getChunk($tpl);
    }else if($x>=1){
    $output.='<p>'.$return[$x].'</p>';

    }
    }
    echo $output;