Created
November 14, 2013 13:20
-
-
Save sonicpunk/7466661 to your computer and use it in GitHub Desktop.
MODX Snippet. This divides the [[*content]] into two separate text blocks that are divided by a chunk. You need to assign to it after how many characters you want to insert your chunk. I used it for inserting an image within the content. I found this script online.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment