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

socket communication between c++/java and sending image
hi,

i have a class in c++ called win32_sockserver which creates socket to java. i am trying

SAP BCS. BPS
Hi all,

I am seeking reading note on the following in BW

BCS, BPS
ADVANCE

I need help on this PHP code
Hi everybody, I made some kind of mistake while editing the code below. As you see, at the 3 row my

my login script page is not working on remote computers
hi my login is working on my computer, but when i tried 2 computers from 2 different locations, they

Help adding strtolower( to my in_array
strtolower i need to add it to this to work, iuno how.

Code: $moderators = array('Admin', 'ba

Get relative path from absolute path
How would one go about getting the relative path to a file from its absolute path?

Pre-Fill out a PHP form...??
This is for work actually (geek squad). We have to fill out this online php form at work over and ov

small inaccuracies
I have this code to convert fractional base 10 into base 2:

while($num > 0)

Automatically Detect Phone Model for WAP Jar Deployment
Making a wap site is fairly simple, but I'd like to know if there's a way to make it so that the wap

Help =( !! Upload Pics [PHP script]
Hello
I need help with a php script [MULTIPLE UPLOAD IMAGES] , where I want to add a feature (wat

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