Skip to Main Content
PHP 5 Kochbuch, Third Edition
book

PHP 5 Kochbuch, Third Edition

by David Sklar, Adam Trachtenberg, Carsten Lucke, Matthias Brusdeylins, Ulrich Speidel, Stephan Schmidt
September 2009
Intermediate to advanced content levelIntermediate to advanced
912 pages
48h 11m
German
O'Reilly Verlag
Content preview from PHP 5 Kochbuch, Third Edition

13.9 Eine Webseite mit Markup versehen

Problem

Sie möchten eine Webseite, zum Beispiel mit Suchergebnissen, so anzeigen, dass bestimmte Wörter hervorgehoben sind.

Lösung

Verwenden Sie preg_replace( ) mit einem Array aus Mustern und Ersetzungstexten:

$patterns = array('\bHund\b/', '\bKatze\b');
$replacements = array('<b style="color:black;background-color=#FFFF00">Hund</b>',
                      '<b style='color:black;background-color=#FF9900">Katze</b>');
while ($page) {
    if (preg_match('{^([^<]*)?(</?[^>]+?>)?(.*)$}',$page,$matches)) {
        print preg_replace($patterns,$replacements,$matches[1]);
        print $matches[2];
        $page = $matches[3];
    }
}

Diskussion

Der mit preg_match( ) verwendete reguläre Ausdruck findet eine Übereinstimmung mit einem möglichst langen Text vor einem HTML-Tag, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP programmieren unter Windows

PHP programmieren unter Windows

Arno Hollosi
CSS Kochbuch, 3rd Edition

CSS Kochbuch, 3rd Edition

Joergen Lang, Christopher Schmitt

Publisher Resources

ISBN: 9783868993271Purchase book