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
user validation always displaying invalid ? please help
Hello,
I have 2 accounts and there is a field in mysql called validated it can be either &quo
echo $rows help
Hi all,
Hopefully something simple that I don't know!
I have this:
Code: [Selec
Problem with HTML form
Hello,
I made a HTML form with some fields and check box.
Everything works fine except whe
php or sql?
Sorry not sure if this is a sql problem or php the following code is supposed to delete data from th
the problem with str_replace
$str="hahahahahahahahahahahahahaha";
$nn=1;
$str=str_replace('ha','MyGod',$str,$nn);
how do i display data on a page from mysql
1 - do i use this code at the top of each of my page i wish to only alow access if there as been a s
Drawing Images in classes
I'm quite new to Object Oriented PHP. What I'm trying to do is draw some images but also have other
Comment Mod System Effects all rows...
Sorry if its confusing but here is whats going on: I have a table in a database called comments and
Strip Slashes Help
I've got a script that i've downloaded of the net to edit multiple fields from my sql database, and
cPanel API
Hi Guys,
Need a little guidance. I'm trying to get my script to communicate with cPanel (or W