Home > OS >  Decode data from URL using PHP
Decode data from URL using PHP

Time:04-22

Im trying to get data from an URL using PHP. The data i get from the URL is like this:

ok {RAH31E telex {Testing1}} {RAH31A telex {Testing1}} {RAH31B telex {Testing1}} {RAH31D telex {Testing1}}

Explaination: ok {SENDER1 TYPE {MESSAGE1}} {SENDER2 TYPE {MESSAGE2}} {SENDER3 TYPE {MESSAGE}} {SENDER4 TYPE {MESSAGE}}

my Plan is, to get the data to my msql table ID | SENDER | TYPE | MESSAGE

The amount of Messages/ Senders etc is variable. It could be one Message up to 10 messages.

I dont have a clue to get this done.

CodePudding user response:

If that sample does actually match your data, and you can't use another format, you can try parsing it with regex:

$data = 'ok {RAH31E telex {Testing1}} {RAH31A telex {Testing1}} {RAH31B telex {Testing1}} {RAH31D telex {Testing1}}';
$pattern = '({(?<SENDER>[^\s] ?)\s(?<TYPE>[^\s] ?)\s{(?<MESSAGE>[^\s] ?)}})';

if(preg_match_all($pattern, $data, $matches, PREG_SET_ORDER )){
    foreach($matches as $match){
        echo $match['SENDER'], ':', $match['TYPE'], ':', $match['MESSAGE'], PHP_EOL;
    }
}

Demo here: https://3v4l.org/mKYRW

  • Related