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
a very simple php header question (sorry!)
Firstly...I do apologise if this annoys anyone....a header error
I'm do not know php &
Database 'Validation'
Hi everyone,
I'm trying to validate the password entered by the user with the password in the dat
small inaccuracies
I have this code to convert fractional base 10 into base 2:
while($num > 0)
Problem executing bash script using shell_exec
Hi there,
I created a bash script file using following code to convert doc documents to pdf using
ALV List Display to point to another report on Double Click
Hi,
I want my ALV List Display to point to another report on Double Click on its line ite
License Issue - service type user
Hi,
I would like to understand the following issue regarding sap license.
I wish to
help finding hacking loopholes
i was attacked by a redirect php injection
my pc is clean of viruses
so i figure that
Attempt to assign property of non-object in...
I'm having issues with the following function in PHP 5...
function getTreeWithChildre
$get problem
Hi Guys ...
can you please tell me what i am doing wrong here
Code: <?php
Why does my crawler script suddenly end with no error?
Hi.
I have written a web crawler script. It will visit a large number of URL's with cURL.