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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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