Jump to content

[SOLVED] breaking apart a block of text


mbeals

Recommended Posts

I have a block of text from a post variable that I need to break apart.  Each 'block' will always start with a string that is nothing but numbers and it will be followed by any number of other strings adhering to 1 of 3 types.  Examples work better so here's some sample data:

 

12345
R0012345678
S0012345678
1Z52x3a1234567
3456
R0012345622
S0012334522

 

Needs to broken into

12345
R0012345678
S0012345678
1Z52x3a1234567
------------------------
3456
R0012345622
S0012334522

 

I have regex that will identify each 'type' independently but I can't get a master string to work.

 

this is what I have:

<?php
$wo = '\b\d+\b';
$rnum = '\b[Rr]\d+\b';
$snum = '\b[ss]\d+\b';
$track = '\b1[zZ]52[xX][\w\d]+\b';

$regex = "/($wo)[$rnum|$snum|$track]+/";

preg_match_all($regex,$text,$matches);
print_r($matches);
?>

 

It just needs to match a $wo, then any number of things matching $rnum, $snum, or $track.

 

Link to comment
https://forums.phpfreaks.com/topic/118447-solved-breaking-apart-a-block-of-text/
Share on other sites

What kind of data structure do you need for this? An array example is below:

 

<pre>
<?php
echo $data = <<<DATA
12345
R0012345678
S0012345678
1Z52x3a1234567
3456
R0012345622
S0012334522
DATA;
echo '<hr>';
$pieces = preg_split('/^(\d+)(?=\s)/m', $data, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($pieces);

?> 
</pre>

 

Your pattern is not working because a character class only matches a character from its pool--there are no patterns. For example, /[a|b]/ will only match "a", "|", or "b".

Thanks, that will work, but ideally I need it as:

 

Array
(
    [0] => Array
        (
            [0] => 12345
            [1] => R0012345678
            [2] => S0012345678
            [3] => 1Z52x3a1234567

        )

    [1] => Array
        (
            [0] => 3456
            [1] => R0012345622
            [2] => S0012334522
        )

)

or even just

(
    [0] => Array
        (
            [0] => 12345
                       R0012345678
                       S0012345678
                      1Z52x3a1234567
        )

    [1] => Array
        (
            [0] => 3456
                       R0012345622
                       S0012334522
        )

)

 

as I need to recursively process each chunk, but I could make the preg split option work.

 

<pre>
<?php
echo $data = <<<DATA
12345
R0012345678
S0012345678
1Z52x3a1234567
3456
R0012345622
S0012334522
DATA;
echo '<hr>';
$pieces = preg_split('/^(?=\d+\s)/m', $data, -1, PREG_SPLIT_NO_EMPTY);
$result = array();
foreach ($pieces as $piece) {
	$result[] = explode("\r\n", trim($piece));
}
print_r($result);

?> 
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.