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

Filling gaps in dates
So i'm doing a query for sales data and my return array looks like this

('date' => '20

Passing variables with pagination - iterating through unique id per link
Hi everyone,
I have seen a few topics like this one. Still cant find the specific thing, so I am

Downloading file (Headers)
I'm trying to make users download a file, but they must wait 60 seconds before it begins.

But

Content-Disposition: attachment; filename=... not working as i thought it should
taken the following code from the php.net site the script is not working.

what is not happeni

need to add "sizes" to shopping cart
Hey guys, I am trying to figure out a way to add a "size" selector on to this bit of code.

Page doesn't expand for content
Hi. I've been testing a query I made. It's here http://lapr1.6te.net/inquerito.php
The page doesn

Table trouble
i have been reading the forum for a few weeks and decided to join. i like the format and the advice

search query in mysql in php problem
my search query wont work, i know, that my codes are correct.please help

Code: [Select]&l

PHP template help
Ok, I don't know if anyone can help me but I am using PHP templates to make a site. I am a newbie an

preg-match with a string and numbers
Hi, I can't get the expression to work for my preg_match, what I'd like is for it to match the strin

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