|
-
Jul 6th, 2002, 02:03 PM
#1
Thread Starter
Hyperactive Member
Replace but how ?
PHP Code:
How can I convert this string
[color=anycolor]any text[/color]
to this string
<font color=anycolor>any text</font>
Can I use str_replace function to do that ? then What is the code ?
Last edited by prokhaled; Jul 6th, 2002 at 02:07 PM.
-
Jul 6th, 2002, 02:11 PM
#2
Fanatic Member
PHP Code:
$str = "[ color=anycolor]any text[/color ]";
$str = str_replace("[color=", "<font color=\"", $str);
$str = str_replace("]", "\">", $str);
$str = str_replace("[/color]","</font>", $str);
will go from
[ color=anycolor]any text[/color ]
to
<font color="anycolor">any text</font>
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Jul 6th, 2002, 02:46 PM
#3
well that is one way but I would use regular expressions, as it is much faster.
PHP Code:
$code = [ color=anycolor]any text[/color ]
$ptn = "/(\[)(color)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/color\])/esiU",
$code=preg_replace($ptn, font, $code);
not sure if that is correct, but is should be pretty close. I will play aorund with it also.
-
Jul 6th, 2002, 03:27 PM
#4
Fanatic Member
this works I think;
PHP Code:
$code = eregi_replace("\[colo[b][/b]r=([^\[]*)\]([^\[]*)\[/col[b][/b]or\]","<font color=\1>\2</font>",$code);
-
Jul 6th, 2002, 03:31 PM
#5
this does
PHP Code:
<?
$code = '[ color=anycolor]any text[/color ]';
$search = array("'\[color'",
"'\]'",
"'\[/color'");
$replace = array("<font color",
">",
"</font");
$text = preg_replace ($search, $replace, $code);
echo $text;
?>
-
Jul 6th, 2002, 05:21 PM
#6
Fanatic Member
ill have to learn how to do those
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Jul 6th, 2002, 09:15 PM
#7
Stuck in the 80s
Originally posted by scoutt
this does
PHP Code:
<?
$code = '[ color=anycolor]any text[/color ]';
$search = array("'\[color'",
"'\]'",
"'\[/color'");
$replace = array("<font color",
">",
"</font");
$text = preg_replace ($search, $replace, $code);
echo $text;
?>
Hey "Standards Boy," ever heard of "<?php" ??
-
Jul 6th, 2002, 09:19 PM
#8
Stuck in the 80s
By the way, scoutt. Is there any way to do it without having it replace every occurance of "]" with ">" in the string?
-
Jul 7th, 2002, 07:48 AM
#9
Stuck in the 80s
Originally posted by The Hobo
By the way, scoutt. Is there any way to do it without having it replace every occurance of "]" with ">" in the string?
Like if we have this:
PHP Code:
$code = '[ color=blue]any text [here] [/color ]';
$search = array("'[color'", "']'", "'[/color'");
$replace = array("<font color", ">", "</font");
$text = preg_replace ($search, $replace, $code);
echo $text;
Will output any text [here>
-
Jul 7th, 2002, 10:17 AM
#10
Thread Starter
Hyperactive Member
G
Yes The Hobo, That is what I need. If you know tell me
-
Jul 7th, 2002, 10:22 AM
#11
Originally posted by The Hobo
By the way, scoutt. Is there any way to do it without having it replace every occurance of "]" with ">" in the string?
oh you caught me. I am so ashamed that I didn't use <?php do you forgive me 
anyway he didn't ask for that. all he wanted was to replace the [ color] tags. there is a way but it gets more involved. what is wrong with what I did, or what kings did?
all I can say is not to put [] around the text
-
Jul 7th, 2002, 05:02 PM
#12
Stuck in the 80s
Originally posted by scoutt
oh you caught me. I am so ashamed that I didn't use <?php do you forgive me 
anyway he didn't ask for that. all he wanted was to replace the [ color] tags. there is a way but it gets more involved. what is wrong with what I did, or what kings did?
all I can say is not to put [] around the text
1) I don't care what he asked for, I asked for something different.
0) You should be ashamed.
2) There's nothing wrong with it, but it doesn't work for what I need.
3) I can't control what characters the user is going to place in.
4) I'll figure it out on my own, since you don't want to get more involved.
5) prokhaled, I'll post my solution when I am finished.
-
Jul 7th, 2002, 05:03 PM
#13
Fanatic Member
-
Jul 7th, 2002, 05:17 PM
#14
Stuck in the 80s
Hey, I'm a programmer, not a math teacher
Here's my solution. It's more involved than a simple replace, but it gets the job done:
PHP Code:
<?php
$ourstring = '[] block head, block [head]. [color=#00ff00]BLOCK HEAD[][/color]<br>
We are [color=$0000ff]COOL[/color] [color=#ff0000], dude![/color]';
$ourstring = colorize($ourstring);
echo $ourstring;
function colorize($thetext) {
if (!strpos($thetext, "[color=") === false) {
do {
$pos = strpos($thetext, "[color=");
if (!$pos === false) {
$epos = strpos($thetext, "]", $pos + 7);
$tag = "[color=" . substr($thetext, $pos + 7, $epos - ($pos + 7)) . "]";
$html = "<font color=\"" . substr($thetext, $pos + 7, $epos - ($pos + 7)) . "\">";
$thetext = str_replace($tag, $html, $thetext);
}
} while(!strpos($thetext, "[color=") === false);
}
$thetext = str_replace("[/color]", "</font>", $thetext);
return $thetext;
}
?>
Feel free to optimize it since I usually take the long way around.
-
Jul 7th, 2002, 10:05 PM
#15
Originally posted by The Hobo
1) I don't care what he asked for, I asked for something different.
0) You should be ashamed.
2) There's nothing wrong with it, but it doesn't work for what I need.
3) I can't control what characters the user is going to place in.
4) I'll figure it out on my own, since you don't want to get more involved.
5) prokhaled, I'll post my solution when I am finished.
you should be able to do something like what kings did and it will leave the [ around text if the user puts it there. it will just replace the [color ones.
-
Jul 7th, 2002, 10:09 PM
#16
Fanatic Member
Originally posted by scoutt
well that is one way but I would use regular expressions, as it is much faster.
PHP Code:
$code = [ color=anycolor]any text[/color ]
$ptn = "/(\[)(color)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/color\])/esiU",
$code=preg_replace($ptn, font, $code);
not sure if that is correct, but is should be pretty close. I will play aorund with it also.
Can you break that down for me? I know preg expressions on a very basic level, and my PHP Book doesnt explain them well.
Really what I need to know is what do the () mean and the ? ^ and \\4 symbols along with the esiU
-
Jul 7th, 2002, 10:12 PM
#17
Fanatic Member
Originally posted by Kings
PHP Code:
$code = eregi_replace("\[colo[b]<b>this works I think;
</b>r=([^\[]*)\]([^\[]*)\[/col[b][/b]or\]","<font color=\1>\2</font>",$code);
[/B]
This would work, but dont you need $ infront of the 1 and 2?
-
Jul 8th, 2002, 07:53 AM
#18
Originally posted by cpradio
Can you break that down for me? I know preg expressions on a very basic level, and my PHP Book doesnt explain them well.
Really what I need to know is what do the () mean and the ? ^ and \\4 symbols along with the esiU
well it gets envloved but I can try. I also have some links to some tutorials on my site about perl reg expressions, pretty much the same thing. the manual goes into as to what \\4menas.
the carat means match exactly and anything in the () means you want to keep and not replace. (.+) means you want any character plus one I think. there is a lot of different things you can use at the . the esiU are really have different meaning. the U is unconditional I think and the "i" is for case sensitivity and can't remember the "e" and "s" are. you might want to check on what I just said as it might be a little off.
-
Jul 8th, 2002, 02:18 PM
#19
here hobo and prokhaled
PHP Code:
<?php
$str = '[color=blue ]any [text][/color ]';
$ptn = '/(\[)(color)(=)(.*)(\])(.*)(\[\/color\])/siU';
$replace = "<font color=\"\\4\"> \\6</font>";
print preg_replace($ptn, $replace, $str);
?>
-
Jul 9th, 2002, 04:15 PM
#20
Stuck in the 80s
Originally posted by scoutt
here hobo and prokhaled
PHP Code:
<?php
$str = '[color=blue ]any [text][/color ]';
$ptn = '/(\[)(color)(=)(.*)(\])(.*)(\[\/color\])/siU';
$replace = "<font color=\"\\4\"> \\6</font>";
print preg_replace($ptn, $replace, $str);
?>
I'll try it when I get home at the end of the week.
-
Jul 9th, 2002, 07:10 PM
#21
Originally posted by The Hobo
I'll try it when I get home at the end of the week.
it was suppose to be
$str = '[ color=blue]any [text][/color ]';
sorry VB took the first part off in my last post.
-
Jul 11th, 2002, 05:21 PM
#22
ugggg I noticed VB took out most of my code. lets see if it works this time.
Code:
<?php
$str = '[ color=blue]any [text][/color]';
$ptn = '/(\[)(color)(=)(.*)(\])(.*)(\[\/color\])/siU';
$replace = "<font color=\"\\4\">\\6</font>";
print preg_replace($ptn, $replace, $str);
?>
-
Nov 30th, 2002, 07:28 PM
#23
Conquistador
[test here]
-
Nov 30th, 2002, 11:57 PM
#24
Stuck in the 80s
Last edited by The Hobo; Dec 1st, 2002 at 12:45 PM.
-
Dec 1st, 2002, 06:17 AM
#25
Conquistador
soz, was just testing if the vbb handled it correctly
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|