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 Like 21 Dislike
Previous forums Next forums
Other forums

HTAccess auto log in?
Hi all.

Too busy driving celebs about lately so I hope someone here can help me out.

I

Users and Groups with PHP Classes
Hi,

I have setup what I hope to be a good working User class. Now I want to introduce group m

Custom list order
Hi there,

I have checked this tutorial and it's great till the point where I want to display

Sub-domains & calling unique content
Hello,

Is there a way use something similar to the $_GET function for a sub-domain? I to be a

how to remove in php string display
My code is $message="We're looking at developing our website."; $body= "My mess

Asset Transfer from One plant to another within same company code
Hi,

Please advise how to transfer one asset from one plant to another plant within same c

Having problemswith multithreading and prime numbers
I have an assignment when I'm suppose to do the following:

Write a multithreaded Java, Pt

Change Web page language
i doing this thing first time but i dont find any suitable solution for it. On the demand of user. I

temporary objects
Until now i thought every temporary object in C++ is created as constant. I'm wondering why my compi

defining website tags
Hi,

What would be the best way to define tags for my site, such as website title, url etc.

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash