PHP str_split按字符长度分成不同的变量

时间:2017-10-02 16:51:02

标签: php string split

我是stackoverflow的新手,我正在学习如何用PHP编程。我在按字符数量分割字符串时遇到问题,这是我到目前为止所做的,但它不起作用..有什么帮助吗?

<?php
$str = "00811561900000000002908201706000003722220170725EF0003002852156290";

$RutCliente = substr($str, 0, 9);
$Credito = substr($str, 9,22);
$CuotaAno = substr($str, 22,26);
$CuotaMes = substr($str, 26,28);
$MontoPagado = substr($str, 28,38);
$FechadePago = substr($str, 38,46);
$MediodePago = substr($str, 46,48);
$BancoDeposito = substr($str, 48,51);
$Voucher = substr($str, 51, 64);

$Partes = str_split($RutCliente,$Credito,$CuotaAno,$CuotaMes,$MontoPagado,$FechadePago,$MediodePago,$BancoDeposito,$Voucher);

print_r($Partes);

?>  

非常感谢

2 个答案:

答案 0 :(得分:0)

str_split不起作用 - 只是将字符串分成同样大小的块。

从你的尝试来看,我想也许你想要这样的东西:

$partes = [
    'RutCliente' => substr($str, 0, 9),
    'Credito' => substr($str, 9,13),
    'CuotaAno' => substr($str, 22,4),
    'CuotaMes' => substr($str, 26,2),
    'MontoPagado' => substr($str, 28,10),
    'FechadePago' => substr($str, 38,8),
    'MediodePago' => substr($str, 46,2),
    'BancoDeposito' => substr($str, 48,3),
    'Voucher' => substr($str, 51),
];

print_r($partes);

请注意,我还对substr调用进行了一些更改 - 看起来你正在开始和结束,但该函数需要一个开始和一个长度

请参阅https://eval.in/872316

答案 1 :(得分:-1)

str_split只接受2个参数,

  

array str_split(string $ string [,int $ split_length = 1])

我希望你试图做到

$Partes = [$RutCliente,$Credito,$CuotaAno,$CuotaMes,$MontoPagado,$FechadePago,$MediodePago,$BancoDeposito,$Voucher];