在数字之间添加 x

我有数字列表


1112

1113

1114

1115

1116-1117

1118-1119

1120

1121-1122

需要显示这些数字,如下所示


1x112

1x113

1x114

1x115

1x120

这些数字随着 - 爆炸而显示


1x116

1x117

1x118

1x119

1x121

1x122


MM们
浏览 156回答 4
4回答

白板的微信

尝试像这样转换为数组。<?php&nbsp;$a = array();$a[] = 1112;$a[] = 1113;$a[] = 1114;$a[] = 1115;$a[] = '1116-1117';$a[] = '1118-1119';$a[] = 1120;$a[] = '1121-1122';$output = array();foreach($a as $key=>$value){&nbsp; &nbsp; if (strpos($value, '-') !== false) {&nbsp; &nbsp; &nbsp; &nbsp; $sub_a = explode('-',$value);&nbsp; &nbsp; &nbsp; &nbsp; foreach($sub_a as $sub_key=>$sub_value){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output[$key][$sub_key] = substr($sub_value, 0, 1).'x'.substr($sub_value, 1);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; $output[$key] = substr($value, 0, 1).'x'.substr($value, 1);&nbsp; &nbsp; }}print_r($output);输出Array(&nbsp; &nbsp; [0] => 1x112&nbsp; &nbsp; [1] => 1x113&nbsp; &nbsp; [2] => 1x114&nbsp; &nbsp; [3] => 1x115&nbsp; &nbsp; [4] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => 1x116&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => 1x117&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [5] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => 1x118&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => 1x119&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [6] => 1x120&nbsp; &nbsp; [7] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => 1x121&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => 1x122&nbsp; &nbsp; &nbsp; &nbsp; ))

跃然一笑

将您的数字转换为字符串,然后您可以操纵它们:$a = 1112;$a = (string) $a;var_dump(substr($a, 0, 1) . 'x' . substr($a, 1)); /* "1x112" */

慕雪6442864

试试下面的代码。它为你工作。<?php&nbsp; &nbsp; $numStr = '1112 1113 1114 1115 1116-1117 1118-1119 1120 1121-1122';&nbsp; &nbsp; $numStr = explode(' ',str_replace('-',' ',$numStr));&nbsp; &nbsp; foreach($numStr as $key => $num) {&nbsp; &nbsp; &nbsp; &nbsp; $numStr[$key] = substr($num, 0, 1) . 'x' . substr($num, 1);&nbsp; &nbsp; }&nbsp; &nbsp; echo $numStr = implode(' ',$numStr);?>输出1x112 1x113 1x114 1x115 1x116 1x117 1x118 1x119 1x120 1x121 1x122&nbsp;

POPMUISE

拆分任何非数字,然后替换部分字符串:<?php$list = '1112 1113 1114 1115 1116-1117 1118-1119 1120 1121-1122';foreach(preg_split('/[^0-9]/', $list) as $number)&nbsp; &nbsp; echo substr_replace($number, 'x', 1, 0), "\n";输出:1x1121x1131x1141x1151x1161x1171x1181x1191x1201x1211x122
打开App,查看更多内容
随时随地看视频慕课网APP