拆分逗号分隔的字符串,但只用逗号分隔
Split a comma separated string but only split by a comma
嗨,我有一个长字符串
0BV,0BW,100,102,108,112,146,163191,192,193,1D94,19339,1A1,1AA,1AE,1AFD,1AG,1AKF.......
我想通过 sub sting 将其显示在页面中
喜欢
0BV,0BW,100,102,108,112,146
163191,192,193,1D94,19339
1A1,1AA,1AE,1AFD,1AG,1AKF
我想做的是从字符串创建子字符串,长度为 100 个字符,但如果第 100 个字符不是逗号,我想检查字符串中的下一个逗号并以此分隔。
我尝试使用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>";
}
请帮忙。提前致谢
取字符串,设置截断位置。如果该位置不包含逗号,则找到该位置之后的第一个逗号并在那里切断。简单
<?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);
(.{99})(?=,),|([^,]*),
您可以获取 easy.See 用于 20
个字符的演示,而不是拆分。
使用 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;
}
万一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
嗨,我有一个长字符串
0BV,0BW,100,102,108,112,146,163191,192,193,1D94,19339,1A1,1AA,1AE,1AFD,1AG,1AKF.......
我想通过 sub sting 将其显示在页面中
喜欢
0BV,0BW,100,102,108,112,146
163191,192,193,1D94,19339
1A1,1AA,1AE,1AFD,1AG,1AKF
我想做的是从字符串创建子字符串,长度为 100 个字符,但如果第 100 个字符不是逗号,我想检查字符串中的下一个逗号并以此分隔。
我尝试使用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>";
}
请帮忙。提前致谢
取字符串,设置截断位置。如果该位置不包含逗号,则找到该位置之后的第一个逗号并在那里切断。简单
<?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);
(.{99})(?=,),|([^,]*),
您可以获取 easy.See 用于 20
个字符的演示,而不是拆分。
使用 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;
}
万一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