dpik71879 2009-10-03 13:47
浏览 33
已采纳

将大字符串转换回数组 - PHP

[Check My EDIT for better Explanation]

I need some help with a very big string i have.

Its like this:

$big_string = "TinteiroID:1#TinteiroLABEL:HP CB335EE#TinteiroREF:CB335EE#TinteiroMARCA:HP#TinteiroGENERO:Tinteiro Preto Reciclado#TinteiroQUANTIDADE:23#FIMPROD#TinteiroID:4#TinteiroLABEL:HP 51633 M#TinteiroREF:51633 M#TinteiroMARCA:HP#TinteiroGENERO:Tinteiro Preto Reciclado#TinteiroQUANTIDADE:12#FIMPROD#";

It as no break lines, but it as white spaces.

If we take a good look this is, they are 2 strings like this:

$splited_string = "TinteiroID:1#TinteiroLABEL:HP CB335EE#TinteiroREF:CB335EE#TinteiroMARCA:HP#TinteiroGENERO:Tinteiro Preto Reciclado#TinteiroQUANTIDADE:23#FIMPROD#";

I think i need a preg_split to search in the $big_string for this:

TinteiroID:[only numbers]#TinteiroLABEL:[any character, except "#"]#TinteiroREF:[any character, except "#"]#TinteiroMARCA:[any character, except "#"]#TinteiroGENERO:[any character, except "#"]#TinteiroQUANTIDADE:[only numbers]#FIMPROD#

I have striped the $splited_string and inside the [ ] square brackets i quote which characters it can be there.

Instead of the [ ] square brackets it should be the RegExpression token for each type of characters that should be accepted. But i know little about this.

And then store each $splited_string in an array $array.

Can anybody give some clues how to accomplish this?

Thanks

EDIT:

I try to explain my logic.

I have this big string (with no break line):

TinteiroID:1#

TinteiroLABEL:HP CB335EE#

TinteiroREF:CB335EE#

TinteiroMARCA:HP#

TinteiroGENERO:Tinteiro Preto Reciclado#

TinteiroQUANTIDADE:23#

FIMPROD#


TinteiroID:4#

TinteiroLABEL:HP 51633 M#

TinteiroREF:51633 M#

TinteiroMARCA:HP#

TinteiroGENERO:Tinteiro Preto Reciclado#

TinteiroQUANTIDADE:12#

FIMPROD#

They can be split into 2 smaller string.

With the preg-split i wanted to assign each splited string that look alike these ones, but with different values:

TinteiroID:[only numbers]#

TinteiroLABEL:[any character, except "#"]#

TinteiroREF:[any character, except "#"]#

TinteiroMARCA:[any character, except "#"]#

TinteiroGENERO:[any character, except "#"]#

TinteiroQUANTIDADE:[only numbers]#

FIMPROD#

Then add each splited string to an array:

Array
(
    [0] => TinteiroID:1#TinteiroLABEL:HP CB335EE#TinteiroREF:CB335EE#TinteiroMARCA:HP#TinteiroGENERO:Tinteiro Preto Reciclado#TinteiroQUANTIDADE:23#FIMPROD#
    [1] => TinteiroID:4#TinteiroLABEL:HP 51633 M#TinteiroREF:51633 M#TinteiroMARCA:HP#TinteiroGENERO:Tinteiro Preto Reciclado#TinteiroQUANTIDADE:12#FIMPROD#
)

And then there will a for each loop to go into every object in the array. [0] [1] ...

Do another RegExpression to collect the values and do something with those values.

Yes its "messy" and takes much CPU but.. I don't have a better ideia :S


EDIT:

Following the Advice:

I've did this code:

$big_string = "TinteiroID:1#TinteiroLABEL:HP CB335EE#TinteiroREF:CB335EE#TinteiroMARCA:HP#TinteiroGENERO:Tinteiro Preto Reciclado#TinteiroQUANTIDADE:23#FIMPROD#TinteiroID:4#TinteiroLABEL:HP 51633 M#TinteiroREF:51633 M#TinteiroMARCA:HP#TinteiroGENERO:Tinteiro Preto Reciclado#TinteiroQUANTIDADE:12#FIMPROD#";CB335EE#TinteiroREF:CB335EE#TinteiroMARCA:HP#TinteiroGENERO:Tinteiro Preto Reciclado#TinteiroQUANTIDADE:23#FIMPROD#";
$array = explode("FIMPROD#", $big_string);

print_r ($array);

It splits the big_string to the at the end of each "FIMPROD#" the Delimiter for each one.

Now i have go on the array, and for each value in it. Do something with it.

I will try that now... I will post something case i managed to do it or not.

  • 写回答

5条回答 默认 最新

  • dtdvbf37193 2009-10-03 14:14
    关注

    why do you need regex here? why don't you just split it twice?

    $num = 6;            # number of elements to in each splited_string
    $out = array();
    foreach ($explode('#', $big_string) as $str) {
        $tmp = explode(':', $str, 2);
        if (count($tmp) == 2) {
            $out[] = $tmp[1];
        }
    }
    $subs = intval(count($out) / $num);  # how many splited_strings in the big string
    for ($i=0; $i<$sub; $i+$num) {
        $each_id = array_slice($out, $i, $i+$num);  # contains six strings
    }
    

    here at each iteration, $each_id would contain six strings, you'd still need to convert first and last elements to integers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗