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
DOMDocument
Hello All,
I need to take an XML document, modify some of the nodes in it and then perform a
Image upload - determine folder
I have an upload script that may be accessed from different directories. Problem is, the script is m
Filtering an Array Based on Value
I have a very simple script set up that pulls data from a database and is output using this code:
Parse error: syntax error, unexpected $end in
hello,
i receive the error :Parse error: syntax error, unexpected $end in C:xamppxampphtdocsh
modifying a property of an object from a different class
I have 2 classes:
- Math
- Distance
in my math class, I have this line:
$distance =
Hyperlink is adding an extra gap to variable
Hi, here's my problem..
I have a php generated page with a hyperlink which opens in a new win
Session problem?!?
Hello All! I am very new to the php world but I am working on fixing things that a previous programm
Saas with SAP R/3
hi experts,
Is SaaS offer available in SAP? for SAP R/3? from where i can get the info a
Batch Related Pricing
Hi, I need some clarity on the batch related pricing
For example the following are the b
Simple MySQL script is not working
Hey guys,
Just familiarizing myself with PHP and MySQL. I am following along in the O'Reilly