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

Shuffle Array
Hi,

I am writing a script for a game that needs players to randomly be assigned a target (ano

pagination - need help on passing of search query.
Hi, i have been trying for days but couldn't get this sorted out. Would like some professional help

Splitting Attributes
SQL> SELECT I_NAME, substr(I_NAME,1,instr(I_NAME,'O')) "First part",

substr(I_NAME, IN

Creation of Raw Exposure manually
SAP Gurus

I am creating Raw Exposure ( Hedge management ) thru T Code TEM10 with followin

How would I protect......
I have a from, actually, a good amount of forms. How can I make it so you can't type the characters:

Procedure with variable number of columns
Hi, I have a procedure that looks like this:

PROCEDURE PROC(p_cursor OUT sys_refcurso

Need help urgant
why down my code ony return one item instead of all the items selected.

$arr = array($date,$

preg_match logical error
Code: <?php
$s = file_get_contents("page.html");
preg_match('/<div cla

PDO returns erroneous columns from within pdt
Hi All,

This is a truly weird behavior:
When using a simple pdo fetch, erroneous columns a

Ignore html and bbcode?
I've created a function to limit the amount of text according to the amount of words.

However

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