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
Creating web pages by php
Hello again,
I was just wondering us there a way to use a php script to create a new web page. I
extract data
Code: <div class="post hentry uncustomized-post-template">
GIS appliction help
I found some tutorials in the internet to develop a map application I don't want to use google maps
PHP time (deadline within one week)
I have a column ['projdue'], which stores the deadline for a project.
I have PHP code and <
email CODING Problem
Email coding problem.
I need to send a section of the info to $tf and the same as a Cc to $em
Anti Spam Code Problems
Ok where do i start? Probably by telling you I have very little working knowledge of PHP and that I
Extracting URL pointer within XML tag
Hi.
I'm trying to extract text between two quotation marks in XML. For example, I want to ex
bind error during socket programming
i am kinda new to socket programming. wrote the below code for a server. but getting the bind error
Storing Values taken from a DB...
Using this code it will generate a short list from my database
<?php
mysql_connect
Displaying image pathname instead of image
Hello
Im trying to upload and then display images from a mysql database - Its only basic and