PHP PHP Explode Strings ausnehmen

ClocxHD

Lt. Junior Grade
Registriert
Aug. 2014
Beiträge
376
Hallo,

ich habe eine INI-Datei, die wie folgt aufgebaut ist:
Code:
ID=["name","Text",Timestamp]

Mit folgendem Code wandel ich diese Einträge in ein Array um, damit ich gut darauf zugreifen kann:
PHP:
<?php

$app->get('/quotes', function () use ($app) {
	$quote_ini = parse_ini_file(INC_ROOT . "/app/data/newquotes.ini");
	$elemente = count($quote_ini);
	$quotes = array();

	foreach ($quote_ini as $item) {
		$item = str_replace("[", "", $item);
		$item = str_replace("]", "", $item);
		$item = explode(',', $item);
		$quotes[] = $item;
	}
	$app->render('home.twig', ['title' => 'Quotes', 'quotes' => $quotes, 'elemente' => $elemente]);
})->name('quotes.get');

Das funktioniert auch soweit.
Wenn jetzt allerdings in einem Satz (oben als "Text" gekennzeichnet) Kommas vorhanden sind, wird der Satz aufgeteilt.

Also bei folgendem Beispiel:
Code:
0=["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]

Würde das werden:
Code:
[
"clocxhd",
"Hallo Welt",
" das ist ein Test!",
"1456505235029"
],

Ich habe es schon folgendermaßen versucht:
PHP:
<?php

$app->get('/quotes', function () use ($app) {
	$quote_ini = parse_ini_file(INC_ROOT . "/app/data/newquotes.ini");
	$elemente = count($quote_ini);
	$quotes = array();

	foreach ($quote_ini as $item) {
		$item = str_replace("[", "", $item);
		$item = str_replace("]", "", $item);
		$item = str_getcsv($item, ',', '"');
		$quotes[] = $item;
	}
	$app->render('home.twig', ['title' => 'Quotes', 'quotes' => $quotes, 'elemente' => $elemente]);
})->name('quotes.get');
Gleiches Ergebnis,

und so:
PHP:
<?php

$app->get('/quotes', function () use ($app) {
	$quote_ini = parse_ini_file(INC_ROOT . "/app/data/newquotes.ini");
	$elemente = count($quote_ini);
	$quotes = array();

	foreach ($quote_ini as $item) {
		$item = str_replace("[", "", $item);
		$item = str_replace("]", "", $item);
		$p = '/[^, ?].*?=(?:".*?"|.*?)(?=,|$)/';
		$item = preg_split($p, $item);
		$quotes[] = $item;
	}
	echo json_encode($quotes);
	//$app->render('home.twig', ['title' => 'Quotes', 'quotes' => $quotes, 'elemente' => $elemente]);
})->name('quotes.get');

Ergebnis:
Code:
[
"clocxhd,Hallo Welt, das ist ein Beispiel,1456505235029"
],

Wie könnte es funktionieren?

Nochmal zur Erklärung, ich möchte folgendes Ergebnis:
Code:
[
"clocxhd",
"Hallo Welt, das ist ein Test!",
"1456505235029"
],

LG,
ClocxHD
 
Komisches Format. Geht das auch anders? Statt daran herumzudoktorn, wäre es besser, das gleich an der Wurzel zu packen und was Ordentliches in die INI zu speichern.

  • JSON
  • PHP serialisiert
  • Optimum DB
  • anderer Trenner
  • Kommas im Namen ausschließen und Name + Zeit als erstes priorisieren, die Nachricht zuletzt und per explode( ",", str, 3 ) splitten
  • ...

Per Regex:
Code:
^\["(?<name>[^"]+)","(?<msg>.*)",(?<time>\d+)\]$

name	[2-9]	`clocxhd`
msg	[12-44]	`Hallo Welt, das ist ein Beispiel`
time	[46-59]	`1456505235029`

Könnte man auch per simplen String-Funktionen lösen, müsste man nur auf die Anführungszeichen achten und dann am Komma splitten.
 
Wie das gespeichert wird, kann ich nicht so einfach beeinflussen.
Das wird von einem ChatBot gemacht.

Dort wird es so gemacht:
Code:
    function e(e, t) {
        var s = $.inidb.GetKeyList("quotes", "").length;
        return $.inidb.set("quotes", s, JSON.stringify([e, t, $.systemTime()])), s
    }
 
Na also. Dann benutz einfach json_decode() beim Wert und fertig ists.
 
Wenn ich das versuche:
Code:
<?php

$app->get('/quotes', function () use ($app) {
	$quote_ini = parse_ini_file(INC_ROOT . "/app/data/newquotes.ini");
	$elemente = count($quote_ini);

	json_decode($quote_ini);
	//$app->render('home.twig', ['title' => 'Quotes', 'quotes' => $quotes, 'elemente' => $elemente]);
})->name('quotes.get');

Bekomme ich folgende Meldung:
Code:
json_decode() expects parameter 1 to be string, array given

Wenn ich das in der for each Schleife mache, bekomme ich nichts angezeigt.
 
Hier versuchst du ein Array mit Struktur der INI-Dateien zu übergeben. Das klappt natürlich nicht, weil JSON komplett anders aufgebaut ist. Dein Code oben sagt dir, dass der Wert einer Variablen in der INI als JSON gespeichert wird. Mach wieder ne Schleife wie im ersten Post, wende auf jeden Wert json_decode() an und hau das auf $quotes drauf. Dann hast du in [0] den Namen, [1] die Nachricht und [2] den Timestamp. Das was dort steht ist ein simples JavaScript Array, was per json_decode() in PHP geparst wird.
 
Ich habe das so versucht:
Code:
<?php

$app->get('/quotes', function () use ($app) {
	$quote_ini = parse_ini_file(INC_ROOT . "/app/data/newquotes.ini");
	$elemente = count($quote_ini);
	$quotes = array();

	foreach ($quote_ini as $item) {
		$item = json_decode($item);
		$quotes[] = $item;
	}
	print_r($quotes);
	//$app->render('home.twig', ['title' => 'Quotes', 'quotes' => $quotes, 'elemente' => $elemente]);
})->name('quotes.get');

Jedoch bekomme ich nur das angezeigt:
Code:
Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => )
 
Sieh dir mal die Werte an und prüf dann die Parameter durch.
Code:
<?php

function get_json_error()
{
	$constants = array();
	foreach( get_defined_constants() as $k => $v )
	{
		if( stripos( $k, "JSON_ERROR_" ) === 0 )
		{
			$constants[$k] = $v;
		}
	}

	$constants = array_flip( $constants );
	$last_error = json_last_error();

	if( isset( $constants[$last_error] ) )
	{
		return $constants[$last_error];
	}
	else
	{
		return "undefined";
	}
}

$file = tempnam(".","");
file_put_contents( $file, <<<INI
[chat]
0=["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]
;1=["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]
;2=["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]
;3=["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]
;4=["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]
;5=["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]
INI
);

$ini = parse_ini_file( $file );
var_dump( $ini );

foreach( $ini as $val )
{
	$e = json_decode( $val );
	var_dump( array( $val, $e, get_json_error() ) );
}

unlink( $file );

Ausgabe:
Code:
array (size=1)
  0 => string '[clocxhd,Hallo Welt, das ist ein Beispiel,1456505235029]' (length=56)

array (size=3)
  0 => string '[clocxhd,Hallo Welt, das ist ein Beispiel,1456505235029]' (length=56)
  1 => null
  2 => string 'JSON_ERROR_SYNTAX' (length=17)

Was fällt auf? Die Anführungszeichen fehlen bereits nach dem Parsen der INI. Gucken wir in die Doku, finden wir den Parameter scanner_mode und die Konstante INI_SCANNER_RAW.

Kurz angewendet:
Code:
<?php

function get_json_error()
{
	$constants = array();
	foreach( get_defined_constants() as $k => $v )
	{
		if( stripos( $k, "JSON_ERROR_" ) === 0 )
		{
			$constants[$k] = $v;
		}
	}

	$constants = array_flip( $constants );
	$last_error = json_last_error();

	if( isset( $constants[$last_error] ) )
	{
		return $constants[$last_error];
	}
	else
	{
		return "undefined";
	}
}

$file = tempnam(".","");
file_put_contents( $file, <<<INI
[chat]
0=["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]
;1=["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]
;2=["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]
;3=["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]
;4=["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]
;5=["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]
INI
);

$ini = parse_ini_file( $file, false, INI_SCANNER_RAW );
var_dump( $ini );

foreach( $ini as $val )
{
	$e = json_decode( $val );
	var_dump( array( $val, $e, get_json_error() ) );
}

unlink( $file );

Ausgabe:
Code:
array (size=1)
  0 => string '["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]' (length=60)

array (size=3)
  0 => string '["clocxhd","Hallo Welt, das ist ein Beispiel",1456505235029]' (length=60)
  1 => 
    array (size=3)
      0 => string 'clocxhd' (length=7)
      1 => string 'Hallo Welt, das ist ein Beispiel' (length=32)
      2 => float 1456505235029
  2 => string 'JSON_ERROR_NONE' (length=15)
 
Zurück
Oben