Search code examples
phpregexpreg-split

Converting big String back to Array - 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.


Solution

  • 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.