Code: Select all
preg_replace("<code>(.*)</code>", "<code>".htmlentities(\\1)."</code>", $tutorial);Example:
- Before function
Code: Select all
<code><?php echo "hello world"; ?></code>Code: Select all
<code><php echo "hello world"; ?></code>Moderator: General Moderators
Code: Select all
preg_replace("<code>(.*)</code>", "<code>".htmlentities(\\1)."</code>", $tutorial);Code: Select all
<code><?php echo "hello world"; ?></code>Code: Select all
<code><php echo "hello world"; ?></code>Code: Select all
<?php
$tutorial = '<code><b>yadda</b></code>';
$pattern = '!<code>(.*)</code>!Use';
$replace = '"<code>".htmlentities(\'$1\')."</code>";';
echo preg_replace($pattern, $replace, $tutorial);
?>Code: Select all
<?php
function cbHtmlentities($a) {
return htmlentities($a[1]);
}
$tutorial = '<code><b>yadda</b></code>';
$pattern = '!(?<=<code>)(.*)(?=</code>)!Us';
echo preg_replace_callback($pattern, 'cbHtmlentities', $tutorial);
?>