Jump to content

Extracting specific data from the database


dekip

Recommended Posts

Hi all,

I have some data in database which is stored something like this:

Quote

S1234567 1234567s S1234567 1234567s S1234567 1234567s S1234567 1234567s

As you may notice there is a pattern. My goal is to extract this data and print it from S to s in a loop.
 

Quote

 

S1234567 1234567s

S1234567 1234567s

S1234567 1234567s

S1234567 1234567s

S1234567 1234567s

 

Can someone help me with this? It should find S and print all to the s.

Not sure what function would be best to use.

Link to comment
Share on other sites

:)

This is the output from an I2C scanner. There should be bunch of 1 and 0. Not 2 or 3, though. :)

The idea is to sort it somehow from S to s. Later to match it with some other stuff, and sort it again.

 

For a start, I should sort, then count the length, later to match, and so on...

Let's say I have a 11101011 on some place here, I should compare it with some other pattern, assign a number, and that number store in db.

Link to comment
Share on other sites

Try

$str = 'S1234567 1234567s S1234567 1234567s S1234567 1234567s S1234567 1234567s';
$p1 = $p2 = 0;

while (($p1 = strpos($str, 'S', $p2)) !== false) {
    $p2 = strpos($str, 's', $p1);
    echo substr($str, $p1, $p2-$p1+1) . '<br>';
}

giving

S1234567 1234567s
S1234567 1234567s
S1234567 1234567s
S1234567 1234567s

 

Link to comment
Share on other sites

Let's say I have this string:

Quote

S0100111W 00000010 sS0100111R 11101011-sS0100111W 00000010 11101111 sS0100111W 00000010 sS0100111R 11101111-sS0100111W 00000010 11111111 sS0100111W 00000011 sS0100111R 10111100-sS0100111W 00000011 10111101 sS0100111W 00000011 sS0100111R 10111101-sS0100111

I would like to make it like this:
 

Quote

 

S0100111W 00000010 s

S0100111R 11101011-s

S0100111W 00000010 11101111 s

S0100111W 00000010 s

S0100111R 11101111-s

S0100111W 00000010 11111111 s

S0100111W 00000011 s

S0100111R 10111100-s

S0100111W 00000011 10111101 s

S0100111W 00000011 s

S0100111R 10111101-sS0100111

 

With this:

Quote

    $p1 = $p2 = 0;
    $sql=mysqli_query($con, "SELECT * FROM table WHERE deviceID=1");
            while($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)){
                $str = $row["data"];
                while (($p1 = strpos($str, 'S', $p2)) !== false) {
                     $p2 = strpos($str, 's', $p1);
                     echo substr($str, $p1, $p2-$p1+1) . '<br>';
                     }
               }

I get this:

Quote

S0100000W 10001000 s
S0100000W 00001000 s
S0100000W 10001000 s
S0100000W 10000000 s
S0100000W 10001000 s
S0100000W 00001000 s
S0100000W 00001000 s
S0100000W 00001000 s
S0100000W 10001000 s
S0100000W 10000000 s
S0100000W 10001000 s
S0100000W 00001000 s

Link to comment
Share on other sites

The main goal is to cut these numbers:
 

Quote

 

S0100111W 00000010 s

S0100111R 11101011-s

S0100111W 00000010 11101111 s

S0100111W 00000010 s

S0100111R 11101111-s

S0100111W 00000010 11111111 s

S0100111W 00000011 s

S0100111R 10111100-s

S0100111W 00000011 10111101 s

S0100111W 00000011 s

S0100111R 10111101-s

S0100111

 

And to process send them to some other tables, etc.

But, these numbers are close to these:
 

Quote

 

S0100111W 00000010 s <-if here is 0

S0100111R 11101011-s      <-than this 11101011 is going to some other table

S0100111W 00000010 11101111 s <-this is not important

S0100111W 00000010 s

S0100111R 11101111-s

S0100111W 00000010 11111111 s

S0100111W 00000011 s <-if here is 1

S0100111R 10111100-s      <-than this 10111100 is going to some other table

S0100111W 00000011 10111101 s

S0100111W 00000011 s

S0100111R 10111101-s

S0100111

 

So where in these numbers last number is say 0, than I take set of numbers (byte array) just under it and do something.

 

PS.

Before everything, I have to check IF after this big S this byte array is 0100111. If not, just ignore.

 

I hope I am a little bit clear. :)

Edited by dekip
Link to comment
Share on other sites

My original should also have checked for no more 's' chars.

$str = 'S0100111W 00000010 sS0100111R 11101011-sS0100111W 00000010 11101111 sS0100111W 00000010 sS0100111R 11101111-sS0100111W 00000010 11111111 sS0100111W 00000011 sS0100111R 10111100-sS0100111W 00000011 10111101 sS0100111W 00000011 sS0100111R 10111101-sS0100111';


$p1 = $p2 = 0;

while (($p1 = strpos($str, 'S', $p2)) !== false) {
    $p2 = strpos($str, 's', $p1);
    if ($p2===false) {                                     // add this check
        $p2 = strlen($str);
    } 
    echo substr($str, $p1, $p2-$p1+1) . '<br>';
}

giving

S0100111W 00000010 s
S0100111R 11101011-s
S0100111W 00000010 11101111 s
S0100111W 00000010 s
S0100111R 11101111-s
S0100111W 00000010 11111111 s
S0100111W 00000011 s
S0100111R 10111100-s
S0100111W 00000011 10111101 s
S0100111W 00000011 s
S0100111R 10111101-s
S0100111

Link to comment
Share on other sites

This one did the trick. Thank you!

I really appreciate your help, but it is nothing if I don't understand what is happening here. Working solution is nice, but understanding is much better.

 

while (($p1 = strpos($str, 'S', $p2)) !== false) {
                $p2 = strpos($str, 's', $p1);
                if ($p2===false) {                                     // add this check
                    $p2 = strlen($str);
                        }
                        echo substr($str, $p1, $p2-$p1+1) . '<br>';
                   }

Row 1:

Finding the first occurrence of an S in $str and if it is not false, than...

Row 2:

Get the length until an s occurs.

Row 3:

This I don't get quite good

Row 4:

Return the length of a $str

Row 6:

Return part of a $str from $p1 (start), to a $p2-$p1+1 (end).

Please correct me where I am wrong.

 

 

Link to comment
Share on other sites

while (($p1 = strpos($str, 'S', $p2)) !== false) {         // while there is a next 'S', get its position
    $p2 = strpos($str, 's', $p1);                          // find position of following 's'
    if ($p2===false) {                                     // if there is no following 's'
        $p2 = strlen($str);                                //    assume its position is the end of the string
    }
    echo substr($str, $p1, $p2-$p1+1) . '<br>';            // echo the characters between the S and s
}

 

Edited by Barand
Link to comment
Share on other sites

Thank you!

I get this and it is clear to me.

Quote

S0100111W 00000010 s                 // this line says it is 0100111 I2C address, and 00000010 here last 0 says it is 0 port
S0100111R 11101011-s                    // this line says it is 0100111 I2C address, and 11101011 are bits I need to put in a specific field in db
S0100111W 00000010 11101111 s // this line I don't need
S0100111W 00000010 s
S0100111R 11101111-s
S0100111W 00000010 11111111 s
S0100111W 00000011 s
S0100111R 10111100-s
S0100111W 00000011 10111101 s
S0100111W 00000011 s
S0100111R 10111101-s
S0100111

However, there are some other things I would need help.

As I point it above...

I should check first two lines, if on both first set of bits are 0100111, than according to last 0 or 1 on the first line second set, take from second line last set of bits.

Let's say, if we take 4th and 5th rows, if on those rows I2C address is the one declared somewhere above is the one I need, than take those 11101111 and put in 0 field.

In 7th and 8th there is 1, so 10111100 should go into field 1.

That array with 3 sets of bits has following s, and is a problem or maybe not. It might be good for a delimiter as it is after every two lines of sets.

 

I messed up with this a week or so, and couldn't make any move, until you helped me. :)


 

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.