Jump to content

split a string in <br>


christa

Recommended Posts

Your number count changes but you can use SUBSTR() to count using the two arguemens of that function - changing it for each requirement number slection to produce the results you want for each. Note: you will just need to define a new variable for each and use the original number as the base for each check using substr().

 

Example:

$test = "phpcoder";

echo substr($test,3)."<br/>";  // prints "coder"

echo substr($test,3,2)."<br/>;  / prints "co"

 

Note: I typed this from memory so double check the syntax but it would work.

For you, just asign 45,3455,66,8900,2,49,88,8,909 to $test and then work with the two arguements and echo the results to new variable names.

 

Link to comment
Share on other sites

I think this is what you're taling about . . .

 

$string = '45,3455,66,8900,2,49,88,8,909';
$array = explode(',', $string);
$i = 1;
foreach( $array as $v ) {
echo trim($v) . ',';
echo $i % 4 === 0 ? '<br>' : '';
$i++;
}

 

Returns:

45,3455,66,8900,

2,49,88,8,

909,

Link to comment
Share on other sites

<?php
$string = "45,3455,66,8900,2,49,88,8,909";
$arr = explode(',',$string); // individual numbers
$arr = array_chunk($arr,4); // groups of 4 numbers

// change each group of 4 to a comma separated string
foreach($arr as $key=>$subarr){
$arr[$key] = implode(',',$subarr);
}

$string = implode(',<br />',$arr); // make string

echo $string;
?>

Link to comment
Share on other sites

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.