christa Posted November 27, 2010 Share Posted November 27, 2010 hi everyone, i have a string as this one: 45,3455,66,8900,2,49,88,8,909 I need to print it with a new line every 4 numbers, as this one: 45,3455,66,8900, 2,49,88,8, 909 How can i do this? many thanks Link to comment https://forums.phpfreaks.com/topic/220014-split-a-string-in/ Share on other sites More sharing options...
MrXHellboy Posted November 27, 2010 Share Posted November 27, 2010 I need to print it with a new line every 4 numbers, as this one: 45,3455,66,8900, 2,49,88,8, 909 As this one ? Thats not 4 numbers on each line. Please provide an obvious example Link to comment https://forums.phpfreaks.com/topic/220014-split-a-string-in/#findComment-1140390 Share on other sites More sharing options...
n1concepts Posted November 27, 2010 Share Posted November 27, 2010 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 More sharing options...
christa Posted November 27, 2010 Author Share Posted November 27, 2010 i tried your code but it prints every number in one line only. I would to obtain this result: 45,3455,66,8900, 2,49,88,8, 909 and so on..... What's not clear? Link to comment https://forums.phpfreaks.com/topic/220014-split-a-string-in/#findComment-1140393 Share on other sites More sharing options...
Pikachu2000 Posted November 27, 2010 Share Posted November 27, 2010 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 More sharing options...
PFMaBiSmAd Posted November 27, 2010 Share Posted November 27, 2010 <?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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.