Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Sting in Array zerlegen
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Frank Kirschner  
View profile   Translate to Translated (View Original)
 More options Mar 8 2011, 8:46 am
Newsgroups: de.comp.lang.php.misc
From: Frank Kirschner <147...@celebrate.de>
Date: Tue, 08 Mar 2011 14:46:12 +0100
Local: Tues, Mar 8 2011 8:46 am
Subject: Sting in Array zerlegen
Hallo,

Ich möchte folgende Zeichenkette zerlegen und in ein Array schreiben
(Jede Zeile endet mit [CR][LF])

<AUTH>fail
<VALIDTO>00.00.0000

und den Array dann so auslesen:

echo $arr[AUTH]; ---> Ausgabe: fail
echo $arr[VALIDTO]; ---> Ausgabe: 00.00.0000

Ich würde als erstes mit explode() als Delimiter \r\n verwenden.
Danach würde ein

echo $arr[0]; ---> <AUTH>fail    ausgeben

Wie mache ich dann weiter, um an oben genanntes Ziel zu kommen?

lg
Frank


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Karl Pflästerer  
View profile   Translate to Translated (View Original)
 More options Mar 8 2011, 10:08 am
Newsgroups: de.comp.lang.php.misc
From: k...@rl.pflaesterer.de (Karl Pflästerer)
Date: Tue, 08 Mar 2011 16:08:24 +0100
Local: Tues, Mar 8 2011 10:08 am
Subject: Re: Sting in Array zerlegen

ich würde stattdessen einen regulären Ausdruck nutzen:

In $s steht dein String:

php > preg_match_all('~^<([^>]+)>(.*)~m', $s, $m, PREG_SET_ORDER);
php > print_r($m);
Array
(
    [0] => Array
        (
            [0] => <AUTH>fail
            [1] => AUTH
            [2] => fail
        )

    [1] => Array
        (
            [0] => <VALIDTO>00.00.0000
            [1] => VALIDTO
            [2] => 00.00.0000
        )

)

  KP


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carsten Wiedmann  
View profile   Translate to Translated (View Original)
 More options Mar 8 2011, 10:49 am
Newsgroups: de.comp.lang.php.misc
From: Carsten Wiedmann <carsten_st...@gmx.de>
Date: Tue, 08 Mar 2011 16:49:36 +0100
Local: Tues, Mar 8 2011 10:49 am
Subject: Re: Sting in Array zerlegen
Am 08.03.2011 14:46, schrieb Frank Kirschner:

> Ich möchte folgende Zeichenkette zerlegen und in ein Array schreiben
> (Jede Zeile endet mit [CR][LF])

> <AUTH>fail
> <VALIDTO>00.00.0000

> und den Array dann so auslesen:

> echo $arr[AUTH]; ---> Ausgabe: fail
> echo $arr[VALIDTO]; ---> Ausgabe: 00.00.0000

Wie Karl würde ich da PCRE nehmen:
| $string = "<AUTH>fail\r\n<VALIDTO>00.00.0000\r\n";
| if (preg_match_all('/(<.+>)(.*)\r\n/',
|     $string, $matches, PREG_PATTERN_ORDER)) {
|     $matches = array_combine($matches[1], $matches[2]);
| } else {
|     $matches = array();
| }
| var_dump($matches);

Ergebnis:
| array(2) {
|   ["<AUTH>"]=>
|   string(4) "fail"
|   ["<VALIDTO>"]=>
|   string(10) "00.00.0000"
| }
Gruß,
Carsten


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carsten Wiedmann  
View profile   Translate to Translated (View Original)
 More options Mar 8 2011, 11:00 am
Newsgroups: de.comp.lang.php.misc
From: Carsten Wiedmann <carsten_st...@gmx.de>
Date: Tue, 08 Mar 2011 17:00:36 +0100
Local: Tues, Mar 8 2011 11:00 am
Subject: Re: Sting in Array zerlegen
Am 08.03.2011 14:46, schrieb Frank Kirschner:
 > Ich möchte folgende Zeichenkette zerlegen und in ein Array schreiben
 > (Jede Zeile endet mit [CR][LF])
 >
 > <AUTH>fail
 > <VALIDTO>00.00.0000
 >
 > und den Array dann so auslesen:
 >
 > echo $arr[AUTH]; ---> Ausgabe: fail
 > echo $arr[VALIDTO]; ---> Ausgabe: 00.00.0000

Wie Karl würde ich da PCRE nehmen:
| $string = "<AUTH>fail\r\n<VALIDTO>00.00.0000\r\n";
| if (preg_match_all('/<(.+)>(.*)\r\n/',
|     $string, $matches, PREG_PATTERN_ORDER)) {
|     $matches = array_combine($matches[1], $matches[2]);
| } else {
|     $matches = array();
| }
| var_dump($matches);

Ergebnis:
| array(2) {
|   ["<AUTH>"]=>
|   string(4) "fail"
|   ["<VALIDTO>"]=>
|   string(10) "00.00.0000"
| }
Gruß,
Carsten


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ethan Sharps  
View profile   Translate to Translated (View Original)
 More options Mar 8 2011, 11:11 am
Newsgroups: de.comp.lang.php.misc
From: Ethan Sharps <inva...@invalid.invalid>
Date: Tue, 08 Mar 2011 17:11:06 +0100
Local: Tues, Mar 8 2011 11:11 am
Subject: Re: Sting in Array zerlegen
Am 08.03.2011 14:46, schrieb Frank Kirschner:

> Hallo,

> Ich möchte folgende Zeichenkette zerlegen und in ein Array schreiben
> (Jede Zeile endet mit [CR][LF])

> <AUTH>fail
> <VALIDTO>00.00.0000

> und den Array dann so auslesen:

> echo $arr[AUTH]; ---> Ausgabe: fail
> echo $arr[VALIDTO]; ---> Ausgabe: 00.00.0000

Du meinst evtl: echo $arr['AUTH']

Das Ist einfach. Ich würde es gleich von Arrayobject ableiten:

class MyDictionary extends ArrayObject
{

     public function __construct($dataString=null)
     {
         parent::__construct();
         if (empty($dataString)) return;
         if (!\preg_match_all("#^<([a-zA-Z]+)>(.+)$#m", $dataString,
$matches))
             return;
         for ($i = 0; $i < \count($matches[0]); ++$i)
         {
             $this[$matches[1][$i]] = $matches[2][$i];
         }
     }

     public function  __toString()
     {
         $result = '';
         $i = 0;
         foreach ($this as $key=>$value)
         {
             if ($i > 0) $result .= "\n";
             else ++$i;
             $result .= \sprintf('<%s>%s', $key, $value);
         }
         return $result;
     }

}

$testStr = "<AUTH>fail
<VALIDTO>00.00.0000";

$data = new \MyDictionary($testStr);
$data['BLUB'] = 'A Value :-)';

echo $data;


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
0liver '0lzo' Seyfert  
View profile   Translate to Translated (View Original)
 More options Mar 8 2011, 12:01 pm
Newsgroups: de.comp.lang.php.misc
From: 0liver '0lzo' Seyfert <himbeert-onl...@gmx.de>
Date: Tue, 8 Mar 2011 18:01:27 +0100
Local: Tues, Mar 8 2011 12:01 pm
Subject: Re: Sting in Array zerlegen
Am Tue, 08 Mar 2011 14:46:12 +0100 schrieb Frank Kirschner:
...

Sting in ein Array zerlegen?

$ar[0] = 'Gordon';
$ar[1] = 'Matthew';
$ar[2] = 'Thomas';
$ar[3] = 'Sumner';

SCNR ;)

> Ich möchte folgende Zeichenkette zerlegen und in ein Array schreiben
> (Jede Zeile endet mit [CR][LF])

[$text = ...]

> <AUTH>fail
> <VALIDTO>00.00.0000

Ohne reg. expr. geht s auch so:

$ar1 = split( "\r\n", $text);
foreach($ar1 as $zl1) {
  $hpos = str_pos( $zl1, '>');
  $hvar = substr( $zl1, 1, $hpos-2);
  $hval = trim(substr( $zl1, $hpos+1));
  $arr[$hvar] = $hval;

}
> und den Array dann so auslesen:

> echo $arr[AUTH]; ---> Ausgabe: fail
> echo $arr[VALIDTO]; ---> Ausgabe: 00.00.0000

Genau, danach ist dann
$arr['AUTH']='fail' und $arr['VALITO']='00.00.000';

:-)
Oliver


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Kirschner  
View profile   Translate to Translated (View Original)
 More options Mar 9 2011, 2:47 am
Newsgroups: de.comp.lang.php.misc
From: Frank Kirschner <147...@celebrate.de>
Date: Wed, 09 Mar 2011 08:47:06 +0100
Local: Wed, Mar 9 2011 2:47 am
Subject: Re: Sting in Array zerlegen
Am 08.03.2011 18:01, schrieb 0liver '0lzo' Seyfert:

Klasse, genau so habe ich das gesucht. Auch die anderen Ansätze sind
sehr elegant. Vielen Dank für die Hilfe.

lg
Frank


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Kirschner  
View profile   Translate to Translated (View Original)
 More options Mar 9 2011, 6:10 am
Newsgroups: de.comp.lang.php.misc
From: Frank Kirschner <147...@celebrate.de>
Date: Wed, 09 Mar 2011 12:10:40 +0100
Local: Wed, Mar 9 2011 6:10 am
Subject: Re: Sting in Array zerlegen
Am 08.03.2011 16:49, schrieb Carsten Wiedmann:

Ist zwar jetzt offtopic:

Der zur zerlegende String stammt von einer mit curl ausgelesenen
Website: http://track.tof.de/cgi-bin/deutsch/benlogin.cgi

Gebe ich diesen String mit 'echo' aus, sehe ich im Quellcode die
Zeilenumbrüche mit [CR][LF]. Warum funktioniert der reguläre Ausdruck
dann nicht? Wird der String wie im Beispiel mit \r\n vorgegeben geht es.
Wo ist der Unterschied?

lg
Frank


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Karl Pflästerer  
View profile   Translate to Translated (View Original)
 More options Mar 9 2011, 6:50 am
Newsgroups: de.comp.lang.php.misc
From: k...@rl.pflaesterer.de (Karl Pflästerer)
Date: Wed, 09 Mar 2011 12:50:25 +0100
Local: Wed, Mar 9 2011 6:50 am
Subject: Re: Sting in Array zerlegen

Irgendwo konvertierst du:

curl -s 'http://track.tof.de/cgi-bin/deutsch/benlogin.cgi'| od -c
0000000   <   A   U   T   H   >   f   a   i   l  \n   <   V   A   L   I
0000020   D   T   O   >   0   0   .   0   0   .   0   0   0   0  \n
0000037

Da ist auch kein \r\n sondern nur ein \n

Je nachdem, was du übergibst, genügst auch ein file_get_contents().

php -r 'preg_match_all("~<([^>]+)>(.*)~m", file_get_contents("http://track.tof.de/cgi-bin/deutsch/benlogin.cgi"), $m); print_r($m);'
Array
(
    [0] => Array
        (
            [0] => <AUTH>fail
            [1] => <VALIDTO>00.00.0000
        )

    [1] => Array
        (
            [0] => AUTH
            [1] => VALIDTO
        )

    [2] => Array
        (
            [0] => fail
            [1] => 00.00.0000
        )

)

  KP


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matthias P. Wuerfl  
View profile   Translate to Translated (View Original)
 More options Mar 9 2011, 8:33 am
Newsgroups: de.comp.lang.php.misc, de.comp.lang.php
Followup-To: de.comp.lang.php
From: "Matthias P. Wuerfl" <matth...@wuerfl.com>
Date: Wed, 09 Mar 2011 14:33:18 +0100
Local: Wed, Mar 9 2011 8:33 am
Subject: Re: Sting in Array zerlegen
Es schrieb Frank Kirschner:

> <AUTH>fail
> <VALIDTO>00.00.0000

Und was soll bei

<VERGLEICH> 2 > 1
<ZWEI>EINS> wahr

kommen?

Grüße, Matthias


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Kirschner  
View profile   Translate to Translated (View Original)
 More options Mar 9 2011, 9:01 am
Newsgroups: de.comp.lang.php.misc
From: Frank Kirschner <147...@celebrate.de>
Date: Wed, 09 Mar 2011 15:01:29 +0100
Local: Wed, Mar 9 2011 9:01 am
Subject: Re: Sting in Array zerlegen
Am 09.03.2011 12:50, schrieb Karl Pflästerer:

Ich übergebe noch ein paar Login Parameter. Aber so wie Du es
beschrieben hast, funktioniert es PERFEKT !!!!
Vielen Vielen Dank!

lg
Frank


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »