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.

RegEx Sql Split

Empfohlene Antworten

Hi und hallo,

ich habe einen SQl String und möchte diesen nach , also Komma splitten.

Das Problem ist, dass in manchen Feldern auch eben Kommas stehen können.

Beispiel:

(26, '', 'Müller, Hans', '');

soll gesplittet werden in

26

[LEER]

Müller, Hans

[LEER]

Kennt jemand den regulären Ausdruck hierfür?

Vielen Dank schonmal

Hey,

in PHP würd ich das so machen:

  
$result = array ();
$spacer = '@@@SPACER@@@';
// Strings mit Quotes heraussuchen
$pattern = '!(["\'])(.*)\\1!U';
preg_match_all($pattern, $string, $matches);
$quoted = isset ($matches[2]) ? $matches[2] : array ();

// Strings mit Quotes maskieren
$string = preg_replace($pattern, $spacer, $string);
$parts = explode(',', $string);
foreach ($parts as $part)
{
if (empty ($part))
continue;

if ($part == $spacer)
{
$result[] = array_shift($quoted);
}
else
{
$result[] = $part;
}
}

[/PHP]

Oder willst du direkt im SQL Statement splitten?

Gruß,

Markus

Hi und danke für die schnelle Antwort.

Ich habe das jetzt mal mit folgendem String getestet:

$string = "(26, '', 'Müller, Hans', '');";

Und folgendes Ergebnis bei print_r($result) bekommen:

Array ( [0] => (26 [1] => @@@SPACER@@@ [2] => @@@SPACER@@@ [3] => @@@SPACER@@@); )

Das ist noch nicht wirklich das, was ich brauche.

Evtl. mach ich auch was falsch?

Hi,

da dein String vor und nach den Kommata ein Space hat, gehts nicht vollständig.

Mit nem trim() in der schleife auf $part beim Vergleich gehen die inneren Ersetzungen. Am Anfang müsstest du noch das ( und ); Aussen entfernen.

Gruß,

Markus

Bearbeitet von kills

Hi kills,

vielen Dank für Deine Mühe.

Es geht jetzt so lala, weil es noch sehr viele Eigenheiten gibt, die man beachten müsste.

Deswegen gehe ich jetzt besser einen anderen Weg und im- bzw. exportiere ein bisschen in der Datenbank hin und her bis alles klappt.

Ich danke Dir vielmals.

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.