Jump to content

Recommended Posts

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?


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

 

 

 

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.

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." ";
}

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

 

 

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! :)

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.