fireundubh Posted May 9, 2007 Share Posted May 9, 2007 I'm creating a horizontal website layout with a fixed height (powered by WordPress.) One of the problems with this approach is that long sections of text do not wrap to additional as-needed new columns. What I would like to do is split a single string into multiple substrings on-the-fly by a specific number of characters. For example, let's say I have a string whose length is 1,000 characters. I would want to split the string into 250-character substrings that could be placed wherever (e.g., <div>$column1</div>, <div>$column2</div>, <div>$column3</div>, <div>$column4</div>.) The split() function appears to do almost what I want, but as you know, that function requires a pattern that I don't have for the strings I want to split. The dirty approach would probably be to use split() in conjunction with introducing an unusual delimiter in the actual string every some number of manually counted characters, but I would like to avoid doing that, if possible. How do I accomplish what I want to do? Quote Link to comment https://forums.phpfreaks.com/topic/50640-solved-splitting-a-string-into-substrings-by-number-of-characters/ Share on other sites More sharing options...
taith Posted May 9, 2007 Share Posted May 9, 2007 like so? that trims it down.... <? function filter_charlimit($string, $length="50"){ if(strlen($string)<$length) return $string; else return trim(substr($string, 0, $length)).'...'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50640-solved-splitting-a-string-into-substrings-by-number-of-characters/#findComment-248909 Share on other sites More sharing options...
paul2463 Posted May 9, 2007 Share Posted May 9, 2007 $strArray = new Array; function splitString($string, $amount) { $start = 0; $end = $amount; while ($number < strlen($string) { $strArray[] = substr($string, $start, $end); $start = $end; $end += $amount; } } // each row of $amount characters are now in an Array called strArray, all you do is call the // array key into your code such as <div>$strArray[0]</div>, <div>$strArray[1]</div>, <div>$strArray[2]</div>, <div>$strArray[3]</div> Quote Link to comment https://forums.phpfreaks.com/topic/50640-solved-splitting-a-string-into-substrings-by-number-of-characters/#findComment-248910 Share on other sites More sharing options...
fireundubh Posted May 9, 2007 Author Share Posted May 9, 2007 So, Paul, while testing your code I found out how to crash Apache, but other than that, I couldn't figure out how to get the code to work. In particular, the $strArray = new Array; bit yields a parse error. I'm not familiar with using arrays at all. Oh, here's the code that effectively crashes Apache by increasing memory usage fast and in large amounts: <?php function splitString($string, $amount) { //$strArray = new Array(); $start = 0; $end = $amount; while ($number < strlen($string)) { $strArray[] = substr($string, $start, $end); $start = $end; $end += $amount; } } $test = "Tessssssssssssstttttttttttttttttttttttttttttttttttttttt"; splitString($test, 10); echo "$strArray[0]"; ?> Probably just a basic neverending loop problem though. Quote Link to comment https://forums.phpfreaks.com/topic/50640-solved-splitting-a-string-into-substrings-by-number-of-characters/#findComment-248939 Share on other sites More sharing options...
jitesh Posted May 9, 2007 Share Posted May 9, 2007 Check this may be helpful. $str = "asdadadaadadasdadadadaadaaddsdfsfsfsfss"; $count = ceil(strlen($str)/250); $str_arr = array(); $next_char = 0; for($i=1;$i<=strlen($count)/250;$i++){ $str_arr[] = substr($str,$next_char ,250); $next_char = $next_char + 250; } // Code to print str foreach($str_arr as $key => $value){ echo $value." "; } Quote Link to comment https://forums.phpfreaks.com/topic/50640-solved-splitting-a-string-into-substrings-by-number-of-characters/#findComment-248956 Share on other sites More sharing options...
paul2463 Posted May 9, 2007 Share Posted May 9, 2007 <?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 is all { $strArray[] = substr($string, $start, $amount); $start = $end; $end = $end + $amount; } return $strArray; } $test = "Tessssssssssssstttttttttttttttttttttttttttttttttttttttt"; $return = splitString($test, 10); //$return = the array of 10 letter strings print_r($return); // echoes out each key of the array echo $return[2];// echoes out tttttttttt ?> sorry some coding snafu's I did it whilst at work without the aid of a testing server, the endless loop was because $ number in the while statement was never instantiated so it was never going to get any bigger $strArray = new Array(); is not needed as the first line inside the while loop creates the array element anyway - apologies and its wrong - so apologies again - need a testing server at work - thrice apologies. Quote Link to comment https://forums.phpfreaks.com/topic/50640-solved-splitting-a-string-into-substrings-by-number-of-characters/#findComment-249221 Share on other sites More sharing options...
fireundubh Posted May 10, 2007 Author Share Posted May 10, 2007 Thanks, Paul. It works great! Now I just need to figure out how to split the string at a space or paragraph character if splitting by number of characters cuts off a word. I'll work on that later tonight. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/50640-solved-splitting-a-string-into-substrings-by-number-of-characters/#findComment-249442 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.