Optimum way

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Boris ©avc

    Optimum way

    What would be the optimum code for replacing string "xxx" with "yyyyyyy" in
    a file? (maybe with str_replace function?)

    Thanks,
    Boris Savc


  • Boris ©avc

    #2
    Re: Optimum way


    "Boris ©avc" <boris.savc@odi s-info.com> wrote in message
    news:7sXDb.75$% x4.7391@news.si ol.net...[color=blue]
    > What would be the optimum code for replacing string "xxx" with "yyyyyyy"[/color]
    in[color=blue]
    > a file? (maybe with str_replace function?)
    >
    > Thanks,
    > Boris Savc
    >
    >[/color]

    but from point where I can find text "START" to point where I can find text
    "STOP" only!

    Thanks to you all,
    Boris Savc



    Comment

    • Pedro Graca

      #3
      Re: Optimum way

      Boris ©avc wrote:[color=blue]
      > "Boris ©avc" <boris.savc@odi s-info.com> wrote in message[color=green]
      >> What would be the optimum code for replacing string "xxx" with "yyyyyyy"
      >> in a file? (maybe with str_replace function?)[/color][/color]
      [color=blue]
      > but from point where I can find text "START" to point where I can find text
      > "STOP" only![/color]

      I might do that using
      Find the position of the first occurrence of a substring in a string


      Replace all occurrences of the search string with the replacement string



      <?php
      // find the first "START" in $contents
      $start = strpos($content s, 'START');
      if ($start !== false) {
      // find first "STOP" *after* the start
      $stop = strpos($content s, 'STOP', $start);
      if ($stop !== false) {
      $partial_conten ts = substr($content s, $start, $stop-$start);
      $partial_conten ts = str_replace('xx x', 'yyyyyyy', $partial_conten ts);
      $contents = substr($content s, 0, $start)
      . $partial_conten ts
      . substr($content s, $stop);
      }
      }
      // done! $contents changed
      ?>
      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      • Boris Savc

        #4
        Re: Optimum way


        "Pedro Graca" <hexkid@hotpop. com> wrote in message
        news:brpjg4$5vh 3r$1@ID-203069.news.uni-berlin.de...[color=blue]
        > Boris ©avc wrote:[color=green]
        > > "Boris ©avc" <boris.savc@odi s-info.com> wrote in message[color=darkred]
        > >> What would be the optimum code for replacing string "xxx" with[/color][/color][/color]
        "yyyyyyy"[color=blue][color=green][color=darkred]
        > >> in a file? (maybe with str_replace function?)[/color][/color]
        >[color=green]
        > > but from point where I can find text "START" to point where I can find[/color][/color]
        text[color=blue][color=green]
        > > "STOP" only![/color]
        >
        > I might do that using
        > http://www.php.net/strpos
        > http://www.php.net/substr
        > http://www.php.net/str_replace
        >
        >
        > <?php
        > // find the first "START" in $contents
        > $start = strpos($content s, 'START');
        > if ($start !== false) {
        > // find first "STOP" *after* the start
        > $stop = strpos($content s, 'STOP', $start);
        > if ($stop !== false) {
        > $partial_conten ts = substr($content s, $start, $stop-$start);
        > $partial_conten ts = str_replace('xx x', 'yyyyyyy', $partial_conten ts);
        > $contents = substr($content s, 0, $start)
        > . $partial_conten ts
        > . substr($content s, $stop);
        > }
        > }
        > // done! $contents changed
        > ?>
        > --
        > --= my mail box only accepts =--
        > --= Content-Type: text/plain =--
        > --= Size below 10001 bytes =--[/color]

        Thanks a million times. Great pointers. Have you got maybe some idea how to
        find "TEXT 9999" (9999 are random numeric fields) with "TEXT 99 TEXT 99"?

        Regards,
        Boris Savc


        Comment

        • Pedro Graca

          #5
          Re: Optimum way

          Boris Savc wrote:[color=blue]
          > Have you got maybe some idea how to
          > find "TEXT 9999" (9999 are random numeric fields) with "TEXT 99 TEXT 99"?[/color]

          ????

          Try regexps:
          Regular Expressions (Perl-Compatible)


          When you start using them you might find "The regex coach" [1] a very
          good tool.

          [1] http://www.weitz.de/regex-coach/
          --
          --= my mail box only accepts =--
          --= Content-Type: text/plain =--
          --= Size below 10001 bytes =--

          Comment

          • Rahul Anand

            #6
            Re: Optimum way

            Hi Boris Savc,

            Using PCRE function preg_replace the required search and replace can
            be implemented by following code:

            [SNIP]

            $contents = "sdkfnajk nsdnfkafk nsdkf a START jsdfnjkan ndk fnkkfg
            xxxkmkxlklxkkxx xx STOP nsdfhvklahdfkh ansbnfj ajdfh xxx fSTOP sdjfh
            sdkfj jasdfkl START dsf xxxxxxx h kljsdfh STOP dskfjl; kj;xxx xxxxx";

            echo preg_replace("/(START.*xxx.*ST OP)/Ue","str_replac e('xxx','yyyy', '$1')",$content s);

            [/SNIP]

            This code will replcae all xxx between START & STOP with yyyy.

            -- Rahul


            "Boris ©avc" <boris.savc@odi s-info.com> wrote in message news:<zPXDb.79$ %x4.7427@news.s iol.net>...[color=blue]
            > "Boris ©avc" <boris.savc@odi s-info.com> wrote in message
            > news:7sXDb.75$% x4.7391@news.si ol.net...[color=green]
            > > What would be the optimum code for replacing string "xxx" with "yyyyyyy"[/color]
            > in[color=green]
            > > a file? (maybe with str_replace function?)
            > >
            > > Thanks,
            > > Boris Savc
            > >
            > >[/color]
            >
            > but from point where I can find text "START" to point where I can find text
            > "STOP" only!
            >
            > Thanks to you all,
            > Boris Savc[/color]

            Comment

            • Boris ©avc

              #7
              Re: Optimum way


              "Pedro Graca" <hexkid@hotpop. com> wrote in message
              news:brpphu$653 t1$1@ID-203069.news.uni-berlin.de...[color=blue]
              > Boris Savc wrote:[color=green]
              > > Have you got maybe some idea how to
              > > find "TEXT 9999" (9999 are random numeric fields) with "TEXT 99 TEXT[/color][/color]
              99"?[color=blue]
              >
              > ????
              >
              > Try regexps:
              > http://www.php.net/PCRE
              >
              > When you start using them you might find "The regex coach" [1] a very
              > good tool.
              >
              > [1] http://www.weitz.de/regex-coach/
              > --
              > --= my mail box only accepts =--
              > --= Content-Type: text/plain =--
              > --= Size below 10001 bytes =--[/color]

              I can find TEXT 9999 and change it to some TEXT but how about changing it to
              TEXT1 99 and TEXT2 99, where 99 and 99 are the digits from original
              expression. Any ideas?

              Thanks,
              Boris Savc


              Comment

              • Pedro Graca

                #8
                Re: Optimum way

                Boris ©avc wrote:[color=blue]
                > I can find TEXT 9999 and change it to some TEXT but how about changing it to
                > TEXT1 99 and TEXT2 99, where 99 and 99 are the digits from original
                > expression. Any ideas?[/color]

                Regexps are your friends :)

                <?php
                $x = 'abc TEXT 5496 def TEXT 0901 ghi';
                $y = preg_replace('/TEXT (\d{2})(\d{2})/', 'TEXT1 $1 TEXT2 $2', $x);
                echo $x, ' -- ', $y, "\n";
                ?>
                --
                --= my mail box only accepts =--
                --= Content-Type: text/plain =--
                --= Size below 10001 bytes =--

                Comment

                • Boris Savc

                  #9
                  Re: Optimum way


                  "Pedro Graca" <hexkid@hotpop. com> wrote in message
                  news:brrspk$6o8 r9$1@ID-203069.news.uni-berlin.de...[color=blue]
                  > Boris ©avc wrote:[color=green]
                  > > I can find TEXT 9999 and change it to some TEXT but how about changing[/color][/color]
                  it to[color=blue][color=green]
                  > > TEXT1 99 and TEXT2 99, where 99 and 99 are the digits from original
                  > > expression. Any ideas?[/color]
                  >
                  > Regexps are your friends :)
                  >
                  > <?php
                  > $x = 'abc TEXT 5496 def TEXT 0901 ghi';
                  > $y = preg_replace('/TEXT (\d{2})(\d{2})/', 'TEXT1 $1 TEXT2 $2', $x);
                  > echo $x, ' -- ', $y, "\n";
                  > ?>
                  > --
                  > --= my mail box only accepts =--
                  > --= Content-Type: text/plain =--
                  > --= Size below 10001 bytes =--[/color]

                  Thanks again Pedro!

                  You have really all the answers. Your tip for Regex Coach was great also.
                  I'm using it all the time. Hope I'll learn my regexps :-) But still one more
                  question: If I want to do the same with characters let's say XTEXTY change
                  with XTEXT TEXT TEXT Y how to do that? (\w finds it, but how to do a
                  replacement?)

                  Regards,
                  Boris Savc


                  Comment

                  • Pedro Graca

                    #10
                    Re: Optimum way

                    Boris Savc wrote:[color=blue]
                    > But still one more
                    > question: If I want to do the same with characters let's say XTEXTY change
                    > with XTEXT TEXT TEXT Y how to do that? (\w finds it, but how to do a
                    > replacement?)[/color]

                    This one I'll let you figure out by yourself :-)

                    Parenthesis in the pattern _grab_ their contents into $1, $2, ...
                    and you can use these references in the substitution part

                    preg_replace('/
                    TEXT # match literal TEXT
                    (\d\d) # match two digits and grab into $1
                    (\d\d) # match two digits and grab into $2
                    /x', ## specify extended syntax for regexp

                    'TEXT $1 TEXT $2', # replace with TEXT (space)
                    # whatever was grabbed into $1
                    # (space) TEXT (space)
                    # and whatever was grabbed into $2
                    'TEXT8764');

                    Happy regexing :-)
                    --
                    --= my mail box only accepts =--
                    --= Content-Type: text/plain =--
                    --= Size below 10001 bytes =--

                    Comment

                    • Boris ©avc

                      #11
                      Re: Optimum way


                      "Boris Savc" <boris.savc@odi s-info.com> wrote in message
                      news:8kfEb.107$ %x4.10059@news. siol.net...[color=blue]
                      >
                      > "Pedro Graca" <hexkid@hotpop. com> wrote in message
                      > news:brrspk$6o8 r9$1@ID-203069.news.uni-berlin.de...[color=green]
                      > > Boris ©avc wrote:[color=darkred]
                      > > > I can find TEXT 9999 and change it to some TEXT but how about changing[/color][/color]
                      > it to[color=green][color=darkred]
                      > > > TEXT1 99 and TEXT2 99, where 99 and 99 are the digits from original
                      > > > expression. Any ideas?[/color]
                      > >
                      > > Regexps are your friends :)
                      > >
                      > > <?php
                      > > $x = 'abc TEXT 5496 def TEXT 0901 ghi';
                      > > $y = preg_replace('/TEXT (\d{2})(\d{2})/', 'TEXT1 $1 TEXT2 $2', $x);
                      > > echo $x, ' -- ', $y, "\n";
                      > > ?>
                      > > --
                      > > --= my mail box only accepts =--
                      > > --= Content-Type: text/plain =--
                      > > --= Size below 10001 bytes =--[/color]
                      >
                      > Thanks again Pedro!
                      >
                      > You have really all the answers. Your tip for Regex Coach was great also.
                      > I'm using it all the time. Hope I'll learn my regexps :-) But still one[/color]
                      more[color=blue]
                      > question: If I want to do the same with characters let's say XTEXTY change
                      > with XTEXT TEXT TEXT Y how to do that? (\w finds it, but how to do a
                      > replacement?)
                      >
                      > Regards,
                      > Boris Savc
                      >
                      >[/color]

                      Sorry. Found it. It's the same way like with numbers.

                      Thanks and have a great day,
                      Boris


                      Comment

                      Working...