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.

[PHP] Regular Expression

Empfohlene Antworten

Hallo zusammen,

ich habe einen String, der z.b. so aufgebaut ist:


$string = "<p>ARTICLE[id=4 format=html]</p>HEADER[id=1 format=html transform=upper]<div><p>ARTICLE[id=4 format=csv]</p></div><br><p>ARTICLE[id=5]</p>";
[/PHP]

Wie kann ich nun dort mit RegEx drauf losgehen, damit ich folgendes Array erhalte:

[PHP]
$articles = array(
array( 'id' => 4, 'format' => 'html'),
array( 'id' => 4, 'format' => 'csv'),
array( 'id' => 5),
);

$header = array(
array( 'id' => 1, 'format' => 'html', 'transform' => 'upper');
);

Ich selbst bin schon auf folgenen Ansatz gekommen, aber ich vermute, dass das doch bestimmt mit 1-2 RegEx schneller geht. Da ich Texte hab, die sehr lang sein können (Mysql longtext) und dort entsprechend sehr oft auch eine dieser "Variablen" vorkommen kann.

Mein Ansatz:


$art_vars = array();

preg_match_all('/ARTICLE[.*]/imsU',$content,$matches = array());
foreach ( $matches as $_match) {
$art_params = array();
$_params = explode( " ", $_match);
foreach ( $_params as $_param) {
$arg = explode( "=", $_param);
$arg_name = $arg[0];
$arg_value = str_replace( array( '"', '\''), '', $arg[1]);
$art_params[$arg_name] = $arg_value;
}
$art_vars[] = $art_params;
}
// weitere Verarbeitung
[/PHP]

Wie gesagt, vermute ich, dass man das doch sicher "schöner" und schneller mit einem oder zwei regex hinbekommt, oder?

Danke für eure Mühe,

Markus

  • 2 Jahre später...

Hab nach Regex CSV und PHP gesucht bin hier gelandet, falls ihr auch mal ein CSV Parsen wollt :

<?

// CSV Notation (Access style): "var1";"var2";"var3";"var4"end_of_line

// Delimiter= ;

// " Quoten mit "

//

// var1 = Ne40";"35_3 // extra ";" dazwischen zu testzwecken

// var2 = german

// var3 = "Black" // extra "

// var4 = 0

$zeichenkette_csv_style = '"Ne40"";""35_3";"german";"""Black""";"0"'."\r\n";

$suchmuster = '/"([^"]{1,}|["]{2}){1,}"/';

preg_match_all($suchmuster, $zeichenkette_csv_style, $treffer);

$resultate = $treffer[0];

//Sauber geschnittene Felder ausgeben

print 'Orginal-Zeichenkette : '.$zeichenkette_csv_style.'<br>';

foreach($resultate as $key => $value){

$var = substr($value,1,-1); //Felder sind mit " eingeschlossen

$var = str_replace('""','"',$var); //Gequotete " entfernen

print $var.'<br>';

}

?>

Wie gesagt, vermute ich, dass man das doch sicher "schöner" und schneller mit einem oder zwei regex hinbekommt, oder?

Ich hab mich mal dran versucht und würd's so machen:


<?php
$string = "<p>ARTICLE[id=4 format=html]</p>HEADER[id=1 format=html transform=upper]<div><p>ARTICLE[id=4 format=csv]</p></div><br><p>ARTICLE[id=5]</p>";


$arrRes = array();

if (preg_match_all('/([\w]+)\[([^\]]+)\]/s', $string, $arrPregRes) > 0) {
foreach ($arrPregRes[1] as $key=>$val) {
if (!array_key_exists($val, $arrRes)) {
$arrRes[$val] = array();
}
if (preg_match_all('/([^\s=]+)=([^\s]+)/s',$arrPregRes[2][$key], $arrInnerPregRes) > 0) {
$arrTmp = array();
foreach ($arrInnerPregRes[1] as $iKey=>$iVal) {
$arrTmp[$iVal] = $arrInnerPregRes[2][$iKey];
}
$arrRes[$val][] = $arrTmp;
}
}
}

echo "<pre>";
print_r($arrRes);
echo "</pre>";
?>
[/PHP]

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Konto

Navigation

Suchen

Suchen

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.