Results 1 to 25 of 25

Thread: Replace but how ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Exclamation 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.

  2. #2
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    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?

  3. #3
    scoutt
    Guest
    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($ptnfont$code); 
    not sure if that is correct, but is should be pretty close. I will play aorund with it also.

  4. #4
    Fanatic Member Kings's Avatar
    Join Date
    Aug 2001
    Posts
    673
    this works I think;
    PHP Code:
    $code eregi_replace("\[colo[b][/b]r=([^\[]*)\]([^\[]*)\[/col[b][/b]or\]","<font color=\1>\2</font>",$code); 
    K i n g s

  5. #5
    scoutt
    Guest
    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;
    ?>

  6. #6
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    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?

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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" ??
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    By the way, scoutt. Is there any way to do it without having it replace every occurance of "]" with ">" in the string?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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>
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    G

    Yes The Hobo, That is what I need. If you know tell me

  11. #11
    scoutt
    Guest
    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

  12. #12
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13
    Fanatic Member Gimlin's Avatar
    Join Date
    Dec 2001
    Location
    Hell
    Posts
    734
    1 0 2345 lol

  14. #14
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  15. #15
    scoutt
    Guest
    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.

  16. #16
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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($ptnfont$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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  17. #17
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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?
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  18. #18
    scoutt
    Guest
    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.

  19. #19
    scoutt
    Guest
    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);

    ?>

  20. #20
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  21. #21
    scoutt
    Guest
    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.

  22. #22
    scoutt
    Guest
    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);
    
    ?>

  23. #23
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    [test here]


  24. #24
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by da_silvy
    [test here]




    Edit: That smiley was supposed to be not
    Last edited by The Hobo; Dec 1st, 2002 at 12:45 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  25. #25
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    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
  •  



Click Here to Expand Forum to Full Width