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 ?
Printable View
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 ?
will go fromPHP Code:$str = "[ color=anycolor]any text[/color ]";
$str = str_replace("[color=", "<font color=\"", $str);
$str = str_replace("]", "\">", $str);
$str = str_replace("[/color]","</font>", $str);
[ color=anycolor]any text[/color ]
to
<font color="anycolor">any text</font>
well that is one way but I would use regular expressions, as it is much faster.
not sure if that is correct, but is should be pretty close. I will play aorund with it also.PHP Code:$code = [ color=anycolor]any text[/color ]
$ptn = "/(\[)(color)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/color\])/esiU",
$code=preg_replace($ptn, font, $code);
this works I think;
PHP Code:$code = eregi_replace("\[colo[b][/b]r=([^\[]*)\]([^\[]*)\[/col[b][/b]or\]","<font color=\1>\2</font>",$code);
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;
?>
ill have to learn how to do those :cool:
Hey "Standards Boy," ever heard of "<?php" ?? :pQuote:
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;
?>
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:Quote:
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?
Will output any text [here> :confused: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;
Yes The Hobo, That is what I need. If you know tell me
oh you caught me. I am so ashamed that I didn't use <?php do you forgive me :pQuote:
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?
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 :p
1) I don't care what he asked for, I asked for something different.Quote:
Originally posted by scoutt
oh you caught me. I am so ashamed that I didn't use <?php do you forgive me :p
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 :p
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.
1 0 2345 lol
Hey, I'm a programmer, not a math teacher :p
Here's my solution. It's more involved than a simple replace, but it gets the job done:
Feel free to optimize it since I usually take the long way around.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;
}
?>
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.Quote:
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.
Can you break that down for me? I know preg expressions on a very basic level, and my PHP Book doesnt explain them well.Quote:
Originally posted by scoutt
well that is one way but I would use regular expressions, as it is much faster.
not sure if that is correct, but is should be pretty close. I will play aorund with it also.PHP Code:$code = [ color=anycolor]any text[/color ]
$ptn = "/(\[)(color)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/color\])/esiU",
$code=preg_replace($ptn, font, $code);
Really what I need to know is what do the () mean and the ? ^ and \\4 symbols along with the esiU
This would work, but dont you need $ infront of the 1 and 2?Quote:
Originally posted by Kings
[/B]PHP Code:$code = eregi_replace("\[colo[b]<b>this works I think;
</b>r=([^\[]*)\]([^\[]*)\[/col[b][/b]or\]","<font color=\1>\2</font>",$code);
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.Quote:
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
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.
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.Quote:
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);
?>
it was suppose to beQuote:
Originally posted by The Hobo
I'll try it when I get home at the end of the week.
$str = '[ color=blue]any [text][/color ]';
sorry VB took the first part off in my last post.
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);
?>
[test here]
:D
:eek:Quote:
Originally posted by da_silvy
[test here]
:D
Edit: That smiley was supposed to be :confused: not :eek:
soz, was just testing if the vbb handled it correctly