note 124391 added to function.preg-replace
| From: | mail at johanvandemerwe dot nl | Date: | Wed, 13 Nov 2019 12:41:54 +0000 |
| Subject: | note 124391 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-213050@lists.php.net to get a copy of this message | ||
Sample for replacing bracketed short-codes
The used short-codes are purely used for educational purposes for they could be shorter as in
'italic' to 'i' or 'bold' to 'b'.
Sample text
----
This sample shows how to have [italic]italic[/italic], [bold]bold[/bold] and
[underline]underlined[/underline] and [strikethrough]striked[/striketrhough] text.
with this function:
<?php
function textDecoration($html)
{
$patterns = [
'/\[(italic)\].*?\[\/\1\] ?/',
'/\[(bold)\].*?\[\/\1\] ?/',
'/\[(underline)\].*?\[\/\1\] ?/'
];
$replacements = [
'<i>$1</i>',
'<strong>$1</strong>',
'<u>$1</u>'
];
return preg_replace($patterns, $replacements, $html);
}
$html = textDecoration($html);
echo $html; // or return
?>
results in:
----
This sample shows how to have <i>italic</i>, <b>bold</b> and
<u>underlined</u> and [strikethrough]striked[/striketrhough] text.
Notice!
There is no [strikethrough]striked[/striketrhough] fallback in the patterns and replacements array
----
Server IP: 208.43.231.9
Probable Submitter: 185.85.3.195 (proxied: 217.121.48.75)
----
Manual Page -- http://php.net/manual/en/function.preg-replace.php
Edit -- https://master.php.net/note/edit/124391
Del: integrated -- https://master.php.net/note/delete/124391/integrated
Del: useless -- https://master.php.net/note/delete/124391/useless
Del: bad code -- https://master.php.net/note/delete/124391/bad+code
Del: spam -- https://master.php.net/note/delete/124391/spam
Del: non-english -- https://master.php.net/note/delete/124391/non-english
Del: in docs -- https://master.php.net/note/delete/124391/in+docs
Del: other reasons-- https://master.php.net/note/delete/124391
Reject -- https://master.php.net/note/reject/124391
Search -- https://master.php.net/manage/user-notes.php