Reputation: 68084
I have string like this:
<a href="http://google.com"> link </a>
[code lang="html" anotherargument="foo"...]
<a href="http://google.com"> link </a>
[/code]
How can I convert the code wrapped between [code...]
and [/code]
to HTML characters?
like this:
<a href="http://google.com"> link </a>
[code lang="html" anotherargument="foo"...]
<a href="http://google.com"> link </a>
[/code]
Upvotes: 0
Views: 856
Reputation: 2552
Try this
preg_match_all('`\[code[^\]]*+]([^\[]*+)\[/code\]`i', $html, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$html = str_replace($match[1], htmlentities($match[1]), $html);
}
Upvotes: 1
Reputation: 58619
You could use regular expressions with a callback - so match the what is between code tags, then replace by running through a function.
Something like this untested code:
$str = preg_replace_callback('/(\[code.+?\])(.+?)(\[\/code\])/', create_function(
'$m',
'return $m[1] . htmlentities($m[2]) . $m[3];'
),$str)
Upvotes: 1
Reputation: 16314
I think htmlspecialchars
or htmlentities
have the functionality you are looking for. Both convert characters to HTML entities.
Upvotes: 1