preg_replace

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

    preg_replace

    Hi,

    I am trying to use preg_replace to replace values in a template XML file
    with values from an array using the following:

    $tmp = preg_replace('/<!-- (\S+) -->/ge', $values[\\1], $line);

    The template file will have lines such as:

    <image source="pics/<!-- pic1 -->" thumb="pics/<!-- thumb1 -->"
    title="<!-- caption1 -->"/>

    And the $values array has values:

    $values[pic1] = "raj1.jpg"
    $values[thumb1] = "traj1.jpg"
    $values[caption1] = "Who is this?"

    Can anyone see where I am going wrong?

    Any help is appreciated...I 've been trying different combinations, but I've
    not found anywhere which uses an array look-up in the replace parameter.

    Thanks in advance!

    --
    Raj Kothary :: one|concept


  • Philip Ronan

    #2
    Re: preg_replace

    "Raj" wrote:
    [color=blue]
    > $tmp = preg_replace('/<!-- (\S+) -->/ge', $values[\\1], $line);
    >
    > The template file will have lines such as:
    >
    > <image source="pics/<!-- pic1 -->" thumb="pics/<!-- thumb1 -->"
    > title="<!-- caption1 -->"/>
    >
    > And the $values array has values:
    >
    > $values[pic1] = "raj1.jpg"
    > $values[thumb1] = "traj1.jpg"
    > $values[caption1] = "Who is this?"
    >
    > Can anyone see where I am going wrong?[/color]

    Try $values['\1'] instead of $values[\\1]. Looks OK otherwise.

    --
    phil [dot] ronan @ virgin [dot] net


    Comment

    • Raj

      #3
      Re: preg_replace

      "Philip Ronan" <invalid@invali d.invalid> wrote in message
      news:BFB37AB7.3 BC85%invalid@in valid.invalid.. .[color=blue]
      > "Raj" wrote:
      >[color=green]
      >> $tmp = preg_replace('/<!-- (\S+) -->/ge', $values[\\1], $line);
      >>
      >> The template file will have lines such as:
      >>
      >> <image source="pics/<!-- pic1 -->" thumb="pics/<!-- thumb1 -->"
      >> title="<!-- caption1 -->"/>
      >>
      >> And the $values array has values:
      >>
      >> $values[pic1] = "raj1.jpg"
      >> $values[thumb1] = "traj1.jpg"
      >> $values[caption1] = "Who is this?"
      >>
      >> Can anyone see where I am going wrong?[/color]
      >
      > Try $values['\1'] instead of $values[\\1]. Looks OK otherwise.[/color]

      Thanks Phil, however it still does not do the replacement. Is there an easy
      way I can print the value of the match? and does the replace function
      substitue greedily? i.e. will it replace all three placeholders on on line?

      Thanks!

      Raj


      Comment

      • Philip Ronan

        #4
        Re: preg_replace

        "Raj" wrote:
        [color=blue]
        > "Philip Ronan" <invalid@invali d.invalid> wrote in message
        > news:BFB37AB7.3 BC85%invalid@in valid.invalid.. .[color=green]
        >>
        >> Try $values['\1'] instead of $values[\\1]. Looks OK otherwise.[/color]
        >
        > Thanks Phil, however it still does not do the replacement. Is there an easy
        > way I can print the value of the match? and does the replace function
        > substitue greedily? i.e. will it replace all three placeholders on on line?[/color]

        I just noticed you're also using unquoted text in the $values declarations.

        Change $values[pic1] to $values['pic1'], etc. You should also put this line
        at the beginning of your script:

        error_reporting (E_ALL);

        That will help you find any other mistakes. You don't need to worry about
        greedy matches because \S will stop matching at the first space character.

        --
        phil [dot] ronan @ virgin [dot] net


        Comment

        • Raj

          #5
          Re: preg_replace


          "Philip Ronan" <invalid@invali d.invalid> wrote in message
          news:BFB38A19.3 BC8D%invalid@in valid.invalid.. .[color=blue]
          > "Raj" wrote:
          >[color=green]
          >> "Philip Ronan" <invalid@invali d.invalid> wrote in message
          >> news:BFB37AB7.3 BC85%invalid@in valid.invalid.. .[color=darkred]
          >>>
          >>> Try $values['\1'] instead of $values[\\1]. Looks OK otherwise.[/color]
          >>
          >> Thanks Phil, however it still does not do the replacement. Is there an
          >> easy
          >> way I can print the value of the match? and does the replace function
          >> substitue greedily? i.e. will it replace all three placeholders on on
          >> line?[/color]
          >
          > I just noticed you're also using unquoted text in the $values
          > declarations.[/color]

          No, that was just an example of the values, for illustration, but they are
          assigned by the $_POST'd values in a loop.
          [color=blue]
          > Change $values[pic1] to $values['pic1'], etc. You should also put this
          > line
          > at the beginning of your script:
          >
          > error_reporting (E_ALL);[/color]

          Thanks, that's a useful command.
          [color=blue]
          > That will help you find any other mistakes. You don't need to worry about
          > greedy matches because \S will stop matching at the first space character.[/color]

          Ok, but I want to replace all occurrences of the placeholder that occur on a
          line with the corresponding value from the $values array.

          Changing $values[\\1] to $values['\1'] gives the following error:

          Notice: Undefined index: \1 in
          /hsphere/local/home/oneconcept/oneconcept.net/honeymoon/gallery_upload. php
          on line 66

          and no substitution occurs.

          Any more ideas? :O)

          Thanks again
          Raj


          Comment

          • Chung Leong

            #6
            Re: preg_replace

            What you need is

            $tmp = preg_replace('/<!-- (\S+) -->/ge', '$values[\1]', $line);

            The quotes are needed around $values[\1] because you want to pass it as
            code to PCRE.

            Comment

            • Raj

              #7
              Re: preg_replace


              "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
              news:1133377396 .630480.258350@ g14g2000cwa.goo glegroups.com.. .[color=blue]
              > What you need is
              >
              > $tmp = preg_replace('/<!-- (\S+) -->/ge', '$values[\1]', $line);
              >
              > The quotes are needed around $values[\1] because you want to pass it as
              > code to PCRE.[/color]

              Thanks Chung...this worked:

              $tmp = preg_replace('/<!-- (\S+) -->/e', '$values["\1"]', $line);

              Thanks all for your help!

              Regards,
              Raj


              Comment

              Working...