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
https://forums.phpfreaks.com/topic/220014-split-a-string-in/#findComment-1140392
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
https://forums.phpfreaks.com/topic/220014-split-a-string-in/#findComment-1140394
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
https://forums.phpfreaks.com/topic/220014-split-a-string-in/#findComment-1140395
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.