Code: Select all
<?php
$input = "Some words.
[foobar1]some code.[/foobar1]
More words.
[foobar2]some code[/foobar2].
Other words.";
?>Code: Select all
// Output:
<b>Some words.
[foobar1]</b><i>some code.</i>[/foobar1]<b>
More words.
[foobar2]</b><i>some code</i><b>[/foobar2].
Other words.</b>;i.e. one action for what's inside tags, another action for the rest of the text.
Thanks!
P.S.
Here's what I tried so far:
Code: Select all
<?php
function dosomething($string, $else = '') {
if (empty($else))
$string = "<b>$string</b>";
else
$string = "<i>$string</i>";
return $string;
}
$codetag = array('\[foobar1\]', '\[foobar2\]');
$codetag_rev = array('\[\/foobar1\]', '\[\/foobar2\]');
for ($i = 0; $i < count($codetag); $i++)
$codetag_array[] = "/([\s\S]*$codetag[$i])([\s\S]+)($codetag_rev[$i])([\s\S]*)/e";
$input_new = preg_replace($codetag_array, 'dosomething("$1") . dosomething("$2","else") .dosomething("$3") . dosomething("$4")', $input);
echo "$input\n<hr>\n$input_new";
// This becomes a huge mess and $2 doesn't get saved from being bold
echo "<hr><hr>";
// Let's try with just one tag
$input_new = preg_replace($codetag_array[0], 'dosomething("$1") . dosomething("$2","else") .dosomething("$3") . dosomething("$4")', $input);
echo "$input\n<hr>\n$input_new";
// The same thing happens
?>