note 96649 added to function.preg-replace
| From: | sergei dot garrison at gmail dot com | Date: | Tue, 09 Mar 2010 21:40:28 +0000 |
| Subject: | note 96649 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-166847@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 97.119.215.41
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- https://master.php.net/note/edit/96649
Del: integrated -- https://master.php.net/note/delete/96649/integrated
Del: useless -- https://master.php.net/note/delete/96649/useless
Del: bad code -- https://master.php.net/note/delete/96649/bad+code
Del: spam -- https://master.php.net/note/delete/96649/spam
Del: non-english -- https://master.php.net/note/delete/96649/non-english
Del: in docs -- https://master.php.net/note/delete/96649/in+docs
Del: other reasons-- https://master.php.net/note/delete/96649
Reject -- https://master.php.net/note/reject/96649
Search -- https://master.php.net/manage/user-notes.php