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>";
Did you know?Explore Trending and Topic pages for more stories like this.
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
Production Order Enterprise Service to Manufacturing Exctn Sys (MES) ???
Hello Experts,
In our current landscape SAP ECC 5.0 is integrated to MES system via PI 7.
type check while uploading
Hi Everyone,
How can I check the exact type of a file while uploading on my site?
Here
Call db table from any PHP file
Hi,
I want to be able to call a database table that will be setup in another file called init
Image loading
Hi Developers,
I'm trying to display an image from images/image.gif getting the url from mysq
Blocking video streaming
Hello everyone,
I post a message here because i didn't find any solution yet.
I just finis
problem in pagination when processing with selectbox form
Hi,
I have a search form, with select boxes, if am selecting the value from the form, it work
Help with form post data and arrays
Hi all,
I am new to this forum... It has been awhile since i have worked with arrays, and i a
parsing error
can anyone explain this syntax error:
Parse error: syntax error, unexpected T_IF
The e
help retrieiving results and doing pagination
Having some trouble trying to get the results to show on more than just one page.
What is ha
Redistributing dependent dlls
Hai all ,
I have created an application in VC++ using VS2008 in a development machine which r