Regular Expressions 101

Community Patterns

There does not seem to be anything here

Community Library Entry

1

Regular Expression
PCRE2 (PHP >=7.3)

/
gamgamtest wesentlich überwiegend
/
gm

Description

<?phpsession_start();class Item{private $productName;private $price;private $availableFrom;private $contactEmail;public function __construct($productName, $price, $availableFrom, $contactEmail){$this->setProductName($productName);$this->setPrice($price);$this->setAvailableFrom($availableFrom);$this->setContactEmail($contactEmail);}public function getProductName(){return $this->productName;}public function setProductName($productName){$this->productName = (string)$productName;}public function getPrice(){return $this->price;}public function setPrice($price){$this->price = (float)$price;}public function getAvailableFrom(){return $this->availableFrom;}public function setAvailableFrom($availableFrom){$this->availableFrom = (string)$availableFrom;}public function getContactEmail(){return $this->contactEmail;}public function setContactEmail($contactEmail){$this->contactEmail = (string)$contactEmail;}}if (!isset($_SESSION['items'])) {$_SESSION['items'] = [];}if ($_SERVER['REQUEST_METHOD'] === 'POST') {if (isset($_POST['save'])) {$productName = $_POST['product_name'] ?? '';$price = $_POST['price'] ?? 0;$availableFrom = $_POST['available_from'] ?? '';$contactEmail = $_POST['contact_email'] ?? '';$item = new Item($productName, $price, $availableFrom, $contactEmail);$key = uniqid('item_', true);$_SESSION['items'][$key] = $item;}if (isset($_POST['delete_one'])) {$deleteKey = $_POST['delete_key'] ?? '';if ($deleteKey !== '' && isset($_SESSION['items'][$deleteKey])) {unset($_SESSION['items'][$deleteKey]);}}if (isset($_POST['delete_all'])) {$_SESSION['items'] = [];}}?><!DOCTYPE html><html lang="de"><head><meta charset="UTF-8"><title>Item-Verwaltung mit Session</title><style>body { font-family: Arial, sans-serif; margin: 20px; }form { margin-bottom: 20px; }table { border-collapse: collapse; width: 100%; max-width: 800px; }th, td { border: 1px solid #aaa; padding: 8px; text-align: left; }th { background-color: #eee; }.form-section { border: 1px solid #ccc; padding: 10px; margin-bottom: 15px; max-width: 500px; }.form-section h2 { margin-top: 0; }</style></head><body><h1>Item-Verwaltung (Session, Formular, Klasse)</h1><div class="form-section"><h2>Neues Produkt speichern</h2><form action="" method="post"><div><label for="product_name">Produktname (text):</label><br><input type="text" id="product_name" name="product_name"value="Gaming Mouse" required></div><br><div><label for="price">Preis in € (number):</label><br><input type="number" id="price" name="price" step="0.01"value="49.99" required></div><br><div><label for="available_from">Verfügbar ab (date):</label><br><input type="date" id="available_from" name="available_from"value="<?php echo date('Y-m-d'); ?>" required></div><br><div><label for="contact_email">Kontakt E-Mail (email):</label><br><input type="email" id="contact_email" name="contact_email"value="info@example.com" required></div><br><button type="submit" name="save">Speichern</button></form></div><div class="form-section"><h2>Eintrag nach Key löschen</h2><form action="" method="post"><label for="delete_key">Key des zu löschenden Eintrags:</label><br><input type="text" id="delete_key" name="delete_key" placeholder="item_..."><br><br><button type="submit" name="delete_one">Löschen nach Key</button></form></div><div class="form-section"><h2>Alle Einträge löschen</h2><form action="" method="post"><button type="submit" name="delete_all">Alle löschen</button></form></div><h2>Gespeicherte Items (Session-Inhalt)</h2><?php if (!empty($_SESSION['items'])): ?><table><thead><tr><th>Key</th><th>Produktname</th><th>Preis (€)</th><th>Verfügbar ab</th><th>Kontakt E-Mail</th></tr></thead><tbody><?php foreach ($_SESSION['items'] as $key => $item): ?><tr><td><?php echo htmlspecialchars($key); ?></td><td><?php echo htmlspecialchars($item->getProductName()); ?></td><td><?php echo number_format($item->getPrice(), 2, ',', ' '); ?></td><td><?php echo htmlspecialchars($item->getAvailableFrom()); ?></td><td><?php echo htmlspecialchars($item->getContactEmail()); ?></td></tr><?php endforeach; ?></tbody></table><?php else: ?><p>Derzeit sind keine Items in der Session gespeichert.</p><?php endif; ?></body></html>
Submitted by anonymous - 4 days ago