Results 1 to 21 of 21

Thread: Replce except text between

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Replce except text between

    How can I replce string with another string except string between [E] and [/E]

    for example: In This string
    $Text="Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";


    I want to replce A with B but ignore A letter that between [E] and [/E]

    How can i do that ?
    Last edited by prokhaled; Aug 2nd, 2002 at 02:44 PM.

  2. #2
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Kinda like how vBulletin Code works?

    PHP Code:
    $text "Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";
    $text preg_replace("/\\[E\\]([A])\\[\\/E\\]","B",$text);
    echo 
    $text
    I am not sure if that is what you want, as your question is not very clear. Can you provide an example of how th output should look like?
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    OUTPUT

    $Text will be

    $Text="Hi how Bre you [E] A A A [/E] Fine B B [E] A any text A [/E]";

    that mean that all A string between [E] and [/E] will not change.

  4. #4
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    It is easier replace the A in the [E] [/E] than outside of them. If I new more preg/reg expressions I would attempt it, but I am pretty novice at them.



    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    any way

    I try your code .. but it give me this error :
    Warning: No ending delimiter '/' found in C:\apache\htdocs\r.php on line 4

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I have an idea how to do it, but I have to go to work. If no one's helped you by then, I'll give it a shot when I get home (around 12:00 EST)
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    PHP Code:
    <?php
    $text1 
    "Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";
    if (
    preg_match('/(.*) (\\[E\\])(.*)(\\[\\/E]) (.*) (\\[E\\])(.*)(\\[\\/E])/'$text1$match)){
      
        
    $rep str_replace("A""B"$match[5]);
        
    $done $match[1].$match[2].$match[3].$match[4].$rep.$match[6].$match[7].$match[8];
        
        echo 
    "<p>".$done;
     
    }else{
      echo 
    "doesn't match";
    }  
    ?>
    Although I don't see the purpose of this.

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    By looking at your code, it will only work with the example text he gave, won't it?

    I think he needs to to work no matter what the text is...
    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
    This is what I came up with:

    PHP Code:
    <?php
        $test1 
    "Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";
        echo 
    "Test1 = " $test1 "<br>After Function, = " convert($test1) . "<br><br>";
        
        
    //THE FUNCTION:
        
    function convert($text) {
            if (!
    strpos($text"[E]") === false) {
                do {
                    
    $pos strpos($text"[E]"$epos);
                    
    $ntext .= str_replace("A""B"substr($text$epos$pos $epos)); // . "<br>";
                    
                    
    $epos strpos($text"[/E]"$pos) + 4;
                    
    $ntext .= substr($text$pos$epos $pos);
                } while (!
    strpos($text"[E]"$epos) === false);
                
            } else {
                
    $ntext str_replace("A""B"$text);
            }        
            
            return 
    $ntext;
        }
    ?>
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Thank you

    But i have another good idea

    How can I replce A letter that between [E] and [/E]

    I try the code of cpradio:

    $text = preg_replace("/\[E\]([A])\[\/E\]","B",$text);

    but it give me error.

    the idea to replace A letter between [E] and [/E] with any strange string for example (!*!*!) and after that replace all A string with B string and after that replace strange string (!*!*!) with A letter

    So what is the error in this code:
    $text = preg_replace("/\[E\]([A])\[\/E\]","B",$text);

  11. #11
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    you are correct Hobo, that will only work with what string he had here.

    prokhaled, Matt's code is missing a /

    $text = preg_replace("/\[E\]([A])\[\/E\]/","B",$text);

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    no effect at all

    I try that

    <?
    $text = "Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";
    $text = preg_replace("/\[E\]([A])\[\/E\]/","B",$text);
    Echo $text;
    ?>

    but no thing changed it's echo the same string without replace any thing

    output:
    Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]

  13. #13
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Thank you

    Originally posted by prokhaled
    But i have another good idea
    Actually, it was Matt's idea...

    So now you want the complete opposite?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    yes opposite

    opposite but short code.

  15. #15
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: yes opposite

    Originally posted by prokhaled
    opposite but short code.
    so read through the manual and find out how to do something on your own for once.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  16. #16
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Re: yes opposite

    Originally posted by The Hobo


    so read through the manual and find out how to do something on your own for once.
    now you know why I did it one way. why waste my time when he changes his mind so much and never does anything on his own.


    $text1 = "Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";

    $text = preg_replace("/(A) /"," B ",$text1);
    echo $text;



  17. #17
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    He's already asked an almost similiar question before...

    http://www.vbforums.com/showthread.p...hreadid=184356
    My evil laugh has a squeak in it.

    kristopherwilson.com

  18. #18
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Personally, my code does not work b/c I did not put time into checking it. I just wrote it as an example to go from.

    If you do a search on many popular forums you will figure out how preg and reg expressions work. It isn't complicated it just takes time and patience.

    Learning is not a bad thing to do. Depending on others however is.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Thanks for all

    I try it

    $text1 = "Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";
    $text = preg_replace("/(A) /"," B ",$text1);
    echo $text;

    output was:
    Hi how Are you [E] B B B [/E] Fine B B [E] B any text B [/E]

    but it must be:
    Hi how Are you [E] B B B [/E] Fine A A [E] B any text B [/E]

  20. #20
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    yup that is what I coded it for. now you have the means you can change it so it will change what you want.

  21. #21
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Thanks for all

    Originally posted by prokhaled
    I try it

    $text1 = "Hi how Are you [E] A A A [/E] Fine A A [E] A any text A [/E]";
    $text = preg_replace("/(A) /"," B ",$text1);
    echo $text;

    output was:
    Hi how Are you [E] B B B [/E] Fine B B [E] B any text B [/E]

    but it must be:
    Hi how Are you [E] B B B [/E] Fine A A [E] B any text B [/E]
    Prokky, I think he's telling you to do it on your own.
    My evil laugh has a squeak in it.

    kristopherwilson.com

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