拆分逗号分隔的字符串,但只能用逗号分隔

时间:2022-09-20 00:17:20

Hi I have a long string

嗨,我有一个很长的字符串

0BV,0BW,100,102,108,112,146,163191,192,193,1D94,19339,1A1,1AA,1AE,1AFD,1AG,1AKF.......

I want to show it in a page by sub sting them

我希望通过对它们进行分析来在页面中显示它

like

喜欢

0BV,0BW,100,102,108,112,146
163191,192,193,1D94,19339
1A1,1AA,1AE,1AFD,1AG,1AKF

What i want to do is create sub strings from the string , length of 100 characters , but if the 100 th character is a not a comma i want to check the next comma in the string and split by that .

我想要做的是从字符串创建子字符串,长度为100个字符,但如果第100个字符不是逗号,我想检查字符串中的下一个逗号并将其拆分。

I tried to use chunk() to split by word count , but since the sub-string lengths are different , it is showing inappropriate in the page

我尝试使用chunk()按字数分割,但由于子字符串长度不同,因此在页面中显示不合适

$db_ocode   = $row["option_code"];

$exclude_options_array =    explode(",",$row["option_code"]);
$exclude_options_chunk_array = array_chunk($exclude_options_array,25);

$exclude_options_string = '';   
foreach($exclude_options_chunk_array as $exclude_options_chunk)
{
    $exclude_options_string .= implode(",",$exclude_options_chunk);
    $exclude_options_string .= "</br>";
}

Please help . thanks in advance

请帮忙 。提前致谢

4 个解决方案

#1


3  

Take the string, set the cutoff position. If that position does not contain a comma then find the first comma after that position and cut off there. Simple

拿绳子,设置截止位置。如果该位置不包含逗号,则在该位置后找到第一个逗号并在那里切断。简单

<?php

$string="0BV,0BW,100,102,108,112,146,163191,192,193,1D94,19339,1A1,1AA,1AE,1AFD";

$cutoff=30;
if($string[$cutoff]!=",")
  $cutoff=strpos($string,",",$cutoff);
echo substr($string,0,$cutoff);

Fiddle

小提琴

#2


2  

(.{99})(?=,),|([^,]*),

Instead of split you can grab the captures which is much easy.See demo for 20 characters.

而不是拆分你可以抓住很容易的捕获。参见20个字符的演示。

https://regex101.com/r/sH8aR8/37

https://regex101.com/r/sH8aR8/37

#3


1  

Using Hanky Panky's answer i was able to provide a complete solution to my Problem , Thank you very much Hanky panky . If my code is not efficient ,Kindly please edit it .

使用Hanky Panky的回答我能够为我的问题提供一个完整的解决方案,非常感谢Hanky panky。如果我的代码效率不高,请编辑它。

$string="0BV,0BW,100,102,108,112,146,163191,192,193,1D94,19339,1A1,1AA,1AE,1AFD";

for($start=0;$start<strlen($string);) {

       $cutoff=30;
       if(isset($string[$start+$cutoff]) && $string[$start+$cutoff]!=",") 
       {
          $cutoff=strpos($string,",",$start+$cutoff);        
       }
       else if(($start+$cutoff) >= strlen($string))
       {
          $cutoff = strlen($string);
       }
       else if($start >= 30)
       {
          $cutoff = $start + $cutoff;
       }

       echo substr($string,$start,$cutoff-$start)."\n";
       $start=$cutoff+1;
    }

#4


0  

In case python

如果是python

ln=0
i=1
str='0BVAa,0BW,100,102,108,112,146,163191,192,193,1D94,19339,1A1,1AA,1AE,1AFD,1AG,1AKF'
for item in str:
    print (item),
    ln=ln+len(item)
    if ln/10>=i and item==',':
        print ""
        i=i+1

#1


3  

Take the string, set the cutoff position. If that position does not contain a comma then find the first comma after that position and cut off there. Simple

拿绳子,设置截止位置。如果该位置不包含逗号,则在该位置后找到第一个逗号并在那里切断。简单

<?php

$string="0BV,0BW,100,102,108,112,146,163191,192,193,1D94,19339,1A1,1AA,1AE,1AFD";

$cutoff=30;
if($string[$cutoff]!=",")
  $cutoff=strpos($string,",",$cutoff);
echo substr($string,0,$cutoff);

Fiddle

小提琴

#2


2  

(.{99})(?=,),|([^,]*),

Instead of split you can grab the captures which is much easy.See demo for 20 characters.

而不是拆分你可以抓住很容易的捕获。参见20个字符的演示。

https://regex101.com/r/sH8aR8/37

https://regex101.com/r/sH8aR8/37

#3


1  

Using Hanky Panky's answer i was able to provide a complete solution to my Problem , Thank you very much Hanky panky . If my code is not efficient ,Kindly please edit it .

使用Hanky Panky的回答我能够为我的问题提供一个完整的解决方案,非常感谢Hanky panky。如果我的代码效率不高,请编辑它。

$string="0BV,0BW,100,102,108,112,146,163191,192,193,1D94,19339,1A1,1AA,1AE,1AFD";

for($start=0;$start<strlen($string);) {

       $cutoff=30;
       if(isset($string[$start+$cutoff]) && $string[$start+$cutoff]!=",") 
       {
          $cutoff=strpos($string,",",$start+$cutoff);        
       }
       else if(($start+$cutoff) >= strlen($string))
       {
          $cutoff = strlen($string);
       }
       else if($start >= 30)
       {
          $cutoff = $start + $cutoff;
       }

       echo substr($string,$start,$cutoff-$start)."\n";
       $start=$cutoff+1;
    }

#4


0  

In case python

如果是python

ln=0
i=1
str='0BVAa,0BW,100,102,108,112,146,163191,192,193,1D94,19339,1A1,1AA,1AE,1AFD,1AG,1AKF'
for item in str:
    print (item),
    ln=ln+len(item)
    if ln/10>=i and item==',':
        print ""
        i=i+1