note 96649 deleted from function.preg-replace by cmb

From: Date: Sun, 22 Sep 2019 08:03:07 +0000
Subject: note 96649 deleted from function.preg-replace by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212310@lists.php.net to get a copy of this message
Note Submitter: sergei dot garrison at gmail dot com ---- If you want to add simple rich text functionality to HTML input fields, preg_replace can be quite handy. For example, if you want users to be able to bold text by typing *text* or italicize it by typing _text_, you can use the following function. <?php function encode(&$text) { $text = preg_replace('/\*([^\*]+)\*/', '<b>\1</b>', $text); $text = preg_replace('/_([^_]+)_/', '<i>\1</i>', $text); return $text; } ?> This works for nested tags, too, although it will not fix nesting mistakes. To make this function more efficient, you could put the delimiters (* and _, in this case) and their HTML tag equivalents in an array and loop through them.

« previous php.notes (#212310) next »