0

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

Fábio Antunes
  • 16,984
  • 18
  • 75
  • 96

5 Answers5

1

This should do then:

"~TinteiroID:(\d+)#TinteiroLABEL:([^#]+)#TinteiroREF:([^#]+)#TinteiroMARCA:([^#]+)#TinteiroGENERO:([^#]+)#TinteiroQUANTIDADE:(\d+)#FIMPROD#~i"
thr
  • 19,160
  • 23
  • 93
  • 130
1

The difficulty of using preg_split() is that you seem to require the string to be split by TWO different expressions: \d+ and [^#]+

I think you should consider splitting the string into smaller pieces and then putting some back together again.

Trying to do the split with one preg_split() will lead to greater complications.

It's also difficult to see the full strings in your question because they have to be scrolled left and right.

Edit: Yes, as Silent Ghost more or less said, you're not splitting on a regular expression with [^#]+, you actually need to split, or better still explode at the # characters.

Then you can put the array back together as two or more smaller strings, perhaps by checking for 'TinteiroID' as the first element of each of the strings.

pavium
  • 14,808
  • 4
  • 33
  • 50
1

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.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
1

Try the code below.

 <?php
    $str = "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#";
    preg_match_all("/([A-Za-z]+)\:([^#]+)/", $str, $matches);
    print_r($matches);
    ?>

You only need one regular expression /([A-Za-z]+)\:([^#]+)/ with preg_match_all function to convert the string into an array. But not sure if it is what you need.

The online PHP regular expression tester will do your help.

unigg
  • 466
  • 3
  • 8
0

I do i get 3 results from the preg_split., while there should be 2?

And with no values?

<?php
$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#";
$array = preg_split("~TinteiroID:(\d+)#TinteiroLABEL:([^#]+)#TinteiroREF:([^#]+)#TinteiroMARCA:([^#]+)#TinteiroGENERO:([^#]+)#TinteiroQUANTIDADE:(\d+)#FIMPROD#~i", $big_string);
print_r ($array);
?>

Output:

Array
(
    [0] => 
    [1] => 
    [2] => 
)
Fábio Antunes
  • 16,984
  • 18
  • 75
  • 96