regular expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shadiya
    New Member
    • Oct 2006
    • 4

    regular expression

    Please could somebody tell me the regular expression in thefollowing case.

    I want to replace variable within {} with some other variable in the string.

    for e.g:-
    $string = "{she} is my friend";
    $replace_variab le = "sita";
    how to replace "{she}" with "sita" using regular expression?
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Why would you use a regular expression when the following will do and faster?
    [php]str_replace('{s he}', $replace_variab le, $string);[/php]
    Ronald :cool:

    Comment

    • Shadiya
      New Member
      • Oct 2006
      • 4

      #3
      Can you please tell me how to use regular expression in this case as i am asked
      to do using it?

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Not very much different[php]preg_replace('/{she}/', $replace_variab le, $string);[/php]
        Ronald :cool:

        Comment

        • Shadiya
          New Member
          • Oct 2006
          • 4

          #5
          thanx a lot for the quick reply :)

          Comment

          • Shadiya
            New Member
            • Oct 2006
            • 4

            #6
            Can you also tell how to replace every thing in the string that starts and ends with curly braces with an array?

            for e.g:
            $string = " {this} chocolate belongs to {that} boy";
            $replace_variab le = array('that', 'this');

            It will be of great help if you could reply soon.

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              YOU HAVE DOUBLE POSTED THIS QUESTION IN THE SAME THREAD!!! DO NOT DO THAT!! READ THE POSTING GUIDELINES BEFORE YOU CONTINUE.
              Well I certainly do not know using a regular expression. That would be a large expression! Maybe someone else can help you here.

              All I know is how to replace, using ARRAYS, strings in the str_replace as in the following example:
              [php]// Provides: You should eat pizza, beer, and ice cream every day
              $phrase = "You should eat fruits, vegetables, and fiber every day.";
              $healthy = array("fruits", "vegetables ", "fiber");
              $yummy = array("pizza", "beer", "ice cream");

              $newphrase = str_replace($he althy, $yummy, $phrase);[/php]
              Ronald :cool:

              Comment

              • ranjithpandi
                New Member
                • Oct 2006
                • 3

                #8
                This will helpful for replacing strings in the braces.

                <?php

                $string = " {this} chocolate belongs to {that} boy";
                $pattern = "/{([^}]+)}/i";
                preg_match_all( $pattern,$strin g,$result);

                $values = $result[1];
                $replace_variab le = array('rep1', 'rep2');
                $newphrase = str_replace($va lues, $replace_variab le, $string);
                echo "<br> String : ".$string;
                echo "<br> New String : ".$newphras e;

                ?>

                Comment

                • ronverdonk
                  Recognized Expert Specialist
                  • Jul 2006
                  • 4259

                  #9
                  I think the original questionn was how to replace the entire {string}, including braces, not just the value within the braces. This sort of replacement is usually done in most templates. So {this} must be replaced by rep1.

                  Ronald :cool:

                  Comment

                  • ranjithpandi
                    New Member
                    • Oct 2006
                    • 3

                    #10
                    This will replace the entire string,includin g the braces.

                    $string = " {this} chocolate belongs to {that} boy";
                    $pattern = "/({[^}]+})/i";
                    preg_match_all( $pattern,$strin g,$result);

                    $values = $result[1];
                    $replace_variab le = array('rep1', 'rep2');
                    $newphrase = str_replace($va lues, $replace_variab le, $string);
                    echo "<br> String : ".$string;
                    echo "<br> New String : ".$newphras e;

                    Ranjith

                    Comment

                    Working...