preg_replace with e modifier, how to use class function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jochen Daum

    preg_replace with e modifier, how to use class function

    Hi,

    I'd like to use something like

    preg_replace(". ..","$this->somefunction(' \\1','\\2');", $content);

    It says "Cannot find function somefunction()"


    I tried some escaping, but didn't work.

    Any ideas?

    Jochen
    --
    Jochen Daum - Cabletalk Group Ltd.
    PHP DB Edit Toolkit -- PHP scripts for building
    database editing interfaces.
    Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

  • CC Zona

    #2
    Re: preg_replace with e modifier, how to use class function

    In article <o59n40t486rjkb ncbn5lvrug7vq94 uc4tq@4ax.com>,
    Jochen Daum <jochen.daum@ca ns.co.nz> wrote:
    [color=blue]
    > preg_replace(". ..","$this->somefunction(' \\1','\\2');", $content);
    >
    > It says "Cannot find function somefunction()"
    >
    >
    > I tried some escaping, but didn't work.
    >
    > Any ideas?[/color]

    Have you tried preg_replace_ca llback <http://php.net/preg-replace-callback> yet? The user notes contain several examples of calling class methods.

    --
    CC

    Comment

    • Jochen Daum

      #3
      Re: preg_replace with e modifier, how to use class function

      Hi

      On Sun, 07 Mar 2004 23:19:53 GMT, CC Zona <cczona@nospam. invalid>
      wrote:
      [color=blue]
      >In article <o59n40t486rjkb ncbn5lvrug7vq94 uc4tq@4ax.com>,
      > Jochen Daum <jochen.daum@ca ns.co.nz> wrote:
      >[color=green]
      >> preg_replace(". ..","$this->somefunction(' \\1','\\2');", $content);
      >>
      >> It says "Cannot find function somefunction()"
      >>
      >>
      >> I tried some escaping, but didn't work.
      >>
      >> Any ideas?[/color]
      >
      >Have you tried preg_replace_ca llback <http://php.net/preg-replace-callback> yet? The user notes contain several examples of calling class methods.[/color]

      Actually not. I expected that the callback gets the 'match' delivered,
      which I expected to be a string, not an array.

      Thanks anyway,

      Jochen

      --
      Jochen Daum - Cabletalk Group Ltd.
      PHP DB Edit Toolkit -- PHP scripts for building
      database editing interfaces.
      Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

      Comment

      • Phil Roberts

        #4
        Re: preg_replace with e modifier, how to use class function

        With total disregard for any kind of safety measures Jochen Daum
        <jochen.daum@ca ns.co.nz> leapt forth and uttered:
        [color=blue]
        > preg_replace("# ...#ies", "\$this->somefunction(' \\1','\\2')", $content);
        >[/color]

        That should work.

        --
        Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/

        Comment

        • Jochen Daum

          #5
          Re: preg_replace with e modifier, how to use class function

          Hi Phil,
          On Mon, 08 Mar 2004 02:03:25 -0600, Phil Roberts
          <philrob@HOLYfl atnetSHIT.net> wrote:
          [color=blue]
          >With total disregard for any kind of safety measures Jochen Daum
          ><jochen.daum@c ans.co.nz> leapt forth and uttered:
          >[color=green]
          >> preg_replace("# ...#ies", "\$this->somefunction(' \\1','\\2')", $content);
          >>[/color]
          >
          >That should work.[/color]

          I tried that. I don't think so.

          Jochen

          --
          Jochen Daum - Cabletalk Group Ltd.
          PHP DB Edit Toolkit -- PHP scripts for building
          database editing interfaces.
          Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

          Comment

          • Pedro Graca

            #6
            Re: preg_replace with e modifier, how to use class function

            Jochen Daum wrote:[color=blue]
            > I'd like to use something like
            >
            > preg_replace(". ..","$this->somefunction(' \\1','\\2');", $content);
            >
            > It says "Cannot find function somefunction()"[/color]
            [color=blue]
            > Any ideas?[/color]

            Maybe this helps ...


            <?php
            class XClass {
            function somefunction($a , $b) {
            echo 'a = ', $a, '; b = ', $b, "\n";
            }

            function anotherfunction () {
            preg_replace('/(.)-(.)/ies', '$this->somefunction(\ '\1\', \'\2\')', 'three-four');
            }
            }

            $x = new XClass;
            preg_replace('/(.)-(.)/ies', '$x->somefunction(\ '\1\', \'\2\')', 'three-four');

            $x->anotherfunctio n();
            ?>

            Output is:
            a = e; b = f
            a = e; b = f
            --
            --= my mail box only accepts =--
            --= Content-Type: text/plain =--
            --= Size below 10001 bytes =--

            Comment

            • Phil Roberts

              #7
              Re: preg_replace with e modifier, how to use class function

              With total disregard for any kind of safety measures Jochen Daum
              <jochen.daum@ca bletalk.co.nz> leapt forth and uttered:
              [color=blue]
              > Hi Phil,
              > On Mon, 08 Mar 2004 02:03:25 -0600, Phil Roberts
              > <philrob@HOLYfl atnetSHIT.net> wrote:
              >[color=green]
              >>With total disregard for any kind of safety measures Jochen Daum
              >><jochen.daum@ cans.co.nz> leapt forth and uttered:
              >>[color=darkred]
              >>> preg_replace("# ...#ies", "\$this->somefunction(' \\1','\\2')",
              >>> $content);
              >>>[/color]
              >>
              >>That should work.[/color]
              >
              > I tried that. I don't think so.
              >
              > Jochen
              >[/color]

              Well then I think you tried it wrong, as I copied that
              implementation from a working example.

              --
              Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/

              Comment

              • Pedro Graca

                #8
                Re: preg_replace with e modifier, how to use class function

                Phil Roberts wrote:[color=blue]
                > With total disregard for any kind of safety measures Jochen Daum
                ><jochen.daum@c abletalk.co.nz> leapt forth and uttered:[color=green]
                >> Hi Phil,
                >> On Mon, 08 Mar 2004 02:03:25 -0600, Phil Roberts
                >> <philrob@HOLYfl atnetSHIT.net> wrote:[color=darkred]
                >>>With total disregard for any kind of safety measures Jochen Daum
                >>><jochen.daum @cans.co.nz> leapt forth and uttered:
                >>>
                >>>> preg_replace("# ...#ies", "\$this->somefunction(' \\1','\\2')",
                >>>> $content);[/color][/color][/color]
                [color=blue][color=green][color=darkred]
                >>>That should work.[/color]
                >>
                >> I tried that. I don't think so.[/color][/color]
                [color=blue]
                > Well then I think you tried it wrong, as I copied that
                > implementation from a working example.[/color]

                Is this inside a method of a class that defines somefunction() as
                another of its methods?
                --
                --= my mail box only accepts =--
                --= Content-Type: text/plain =--
                --= Size below 10001 bytes =--

                Comment

                • Phil Roberts

                  #9
                  Re: preg_replace with e modifier, how to use class function

                  With total disregard for any kind of safety measures Pedro Graca
                  <hexkid@hotpop. com> leapt forth and uttered:
                  [color=blue]
                  > Is this inside a method of a class that defines somefunction() as
                  > another of its methods?[/color]

                  Yes. Here is the original source (taken from the invision board code):

                  $txt = preg_replace( "#\[url\](\S+?)\[/url\]#ie", "\$this->regex_build_ur l(array('html' => '\\1', 'show' => '\\1'))", $txt );

                  The 'e' switch after the regex string instructs preg_replace to
                  evaluate the replacement string as PHP code.

                  --
                  Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/

                  Comment

                  Working...