Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

suchen / ersetzen php

Empfohlene Antworten

Veröffentlicht

Hallo,

hab einen csv String...

bla,bla,"bla,bla,bla",bla,bla,"bla,bla",bla

..jetzt möchte ich mit explode($meinString,",") den String nach Kommas trennen, aber nur nach Kommas die nicht zwischen " " stehen. Wie mach ich das am elegantesten?

Vielen Dank

Jens

Well, mir fällt grad nix blöderes ein, als den String mal nach \" zu exploden und dann, jenachdem, ob am anfang des strings ein " vorkam alle ungeraden, andernfalls alle geraden Arrayelemente nochmal nach Komma zu trennen und das ganze zum Ergebnis-Array hinzufügen:


//$meinString enthält das zu splittende Array


//nach " trennen
$tmp_1 = explode($meinString , "\"");

//wenn der erste Teil kein "bla,bla" war, dann nimm alle geraden Elemente
//ansonsten fange erst beim nächsten an und kopiere das erste "bla,bla" samt
//Anführungszeichen ins Ergebnis-Array
if (strpos($meinString,"\"") > strpos($meinString,",") )
$i = 0;
else {
array_push($erg, "\"".$tmp_1[0]."\"");
$i = 1;
}

if (; $i < count($tmp_1); $i+=2) {

//"Normale" Teile ohne " nach Komma trennen und ins Ergebnis pushen
$tmp_2 = explode($tmp_1[$i] , ",");
foreach ($tmp_2 as $tmp_3)
array_push($erg,$tmp_3);

//Wenn danach noch ein "-Teil kommen könnte, diesen mit " ins Ergebnis
if ($i+1 < count($tmp_1)
array_push($erg, "\"".$tmp[$i+1]."\"");
}
[/PHP]

Sorry, Code ist unübersichtlich, aber ich hab net soviel Zeit gehabt

HTH

mfg kLeiner_HobBes

Noch eleganter geht's so:


<?php
$text='bla,bla,"bla,bla,bla",bla,bla,"bla,bla",bla';
$pattern="/\"([^\"]*)\"|([^,]*)/";
preg_match_all( $pattern, $text, $array );
$array = array_filter( array_merge( $array[1], $array[2] ) );
print_r($array);
?>
[/PHP]

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.