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
how can we add data from dynamic fields to mysql db in php
i have a script which i found on the internet. i modify that script according to my needs. what is i
The IE or FF detector
Hello Friends,
Problem of today ( we seek for php help ) as following ,
we know all th
How can I uploading Transactional Data from Legacy to New SAP system
Hell Gurus,
I am overwhelmed with questions on how to migrate huge volume of Sales Orders
Libraries in C++
Hi all,
I have two libraries. one is based targeted on linux platform and uses another li
Not showing whole name with mail () script
I sent up a simple mail form with the PHP mail() script. One problem is when it sends an email with
Checking BOM Authorization Group
Hai Friends,
I have developed a mulitilevel BOM display report. End users have been assig
Shuffle Array
Hi,
I am writing a script for a game that needs players to randomly be assigned a target (ano
Need a fuction to count entries in a field
Hi
I need a fuction to count how many times a email address is entered in to a field.
I ma
getting Vars to pass to next page.
Hello all,
I have a confusing situation on my hands, i am a member of a gaming community and we
search function
HI guys,
if anyone could point us in the right direction of how to do this, or provide some t