Different actions for different parts of a string
Posted on
16th Feb 2014 07:03 pm by
admin
<?php
function dosomething($string, $else = '') {
if (empty($else))
$string = "<b>$string</b>";
else
$string = "<i>$string</i>";
return $string;
}
$input = "Some words.
[code]some code.[/code]
More words.
[quote]some code[/quote].
Other words.";
$codetag = array('[code]', '[quote]');
$codetag_rev = array('[/code]', '[/quote]');
for ($i = 0; $i < count($codetag); $i++)
$codetag_array[] = "/([sS]*$codetag[$i])([sS]+)($codetag_rev[$i])([sS]*)/e";
$input_new = preg_replace($codetag_array, 'dosomething("$1") . dosomething("$2","else") .dosomething("$3") . dosomething("$4")', $input);
echo "$inputn<hr>n$input_new";
// This becomes a huge mess and $2 doesn't get saved from being bold
echo "<hr><hr>";
// It does work fine if I only try it on one tag
$input_new = preg_replace($codetag_array[0], 'dosomething("$1") . dosomething("$2","else") .dosomething("$3") . dosomething("$4")', $input);
echo "$inputn<hr>n$input_new";
// $2 is indeed saved from turning bold
?>
So my only question is - how do I do this for multiple tags?
Or, if you want, you can simply ignore everything until now and just tell me how do I turn $input into:
// Output:
<b>Some words.
[code]</b><i>some code.</i>[/code]<b>
More words.
[quote]</b><i>some code</i><b>[/quote].
Other words.</b>;
Thanks
No comments posted yet
Your Answer:
Login to answer
114
21
Other forums
help countdown timers
hello every one,
I'd like to know how to insert many countdowns in the same page.
The dura
Varible in trigger - refering to correct schema
Hello
Im having some issues with my trigger.
What I want to do is call opon a differ
array help
Does anyone know how to require 10 text fields for individual grades and output class average? (10 p
A Few Questions
Hi, I want to ask a few questions that I can't seem to find..
1) I'm creating a "shortcu
frame help
on the bottom frame is menu.html
Code: <body><form action='link.php' met
Pls help with PHP
Pls can anyone direct me on how to create a user account for a customer. For example i want know if
HTML form problem
Ive made a form to that i want to send to my email via PHP. but when it send only the name's etc sen
getting weird error..
okay, I'm getting an error on this particular function, something about the syntax in the mysql LIMI
Two warning messages
Quote<b>Warning</b>: mysql_real_escape_string() expects parameter 1 to
Join Query Help
Hi all,
I am having problems with the below code, which we shall call 'my first join query'!