PREG matching

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

    PREG matching

    I'm trying to match:
    <image=1,righ t>
    - or -
    <image=1>

    where the 1 and 'right' become parameters.

    So far I have:
    preg_match_all( '#<image=(.+?), (.+?)> | <image=([^>]+)#msi', $body,
    $image_tags);

    and

    $image_count = preg_match_all( '#<image=([^>]+)#msi', $body, $image_tags);

    I'm trying to see if I can get them both into the one regexep, I assume
    I have to use an alternative | somewhere, just not sure where.

    Can anyone help?
  • Logical

    #2
    Re: PREG matching

    Logical wrote:
    [color=blue]
    > I'm trying to match:
    > <image=1,righ t>
    > - or -
    > <image=1>
    >
    > where the 1 and 'right' become parameters.
    >
    > So far I have:
    > preg_match_all( '#<image=(.+?), (.+?)> | <image=([^>]+)#msi', $body,
    > $image_tags);
    >
    > and
    >
    > $image_count = preg_match_all( '#<image=([^>]+)#msi', $body, $image_tags);
    >
    > I'm trying to see if I can get them both into the one regexep, I assume
    > I have to use an alternative | somewhere, just not sure where.
    >
    > Can anyone help?[/color]

    Of course I had to make a mistake in pasting my code.

    That should have read:
    preg_match_all( '#<image=(.+?), (.+?)>#msi', $body, $image_tags);
    and
    preg_match_all( '#<image=([^>]+)#msi', $body, $image_tags);

    Comment

    • Michael Fesser

      #3
      Re: PREG matching

      .oO(Logical)
      [color=blue]
      >That should have read:
      > preg_match_all( '#<image=(.+?), (.+?)>#msi', $body, $image_tags);
      > and
      > preg_match_all( '#<image=([^>]+)#msi', $body, $image_tags);[/color]

      Try (untested)

      preg_match_all( '#<image=(.+)(, (.+))?>#isU', $body, $image_tags);

      Micha

      Comment

      • kingofkolt

        #4
        Re: PREG matching

        "Logical" <me@privacy.net > wrote in message
        news:413c25c9$0 $31704$61c65585 @un-2park-reader-01.sydney.pipen etworks.com.au. ..[color=blue]
        > I'm trying to match:
        > <image=1,righ t>
        > - or -
        > <image=1>
        >
        > where the 1 and 'right' become parameters.
        >
        > So far I have:
        > preg_match_all( '#<image=(.+?), (.+?)> | <image=([^>]+)#msi', $body,
        > $image_tags);
        >
        > and
        >
        > $image_count = preg_match_all( '#<image=([^>]+)#msi', $body, $image_tags);
        >
        > I'm trying to see if I can get them both into the one regexep, I assume
        > I have to use an alternative | somewhere, just not sure where.
        >
        > Can anyone help?[/color]

        I could be wrong, but wouldn't it be easier (or anyways take less processing
        time) to do a str_replace()? Ex:

        <?php
        $params=explode ( "|",
        str_replace(
        array( "<image=", ",", ">" ), array( "", "|", "" ), $body
        )
        );

        print_r($params );
        ?>

        - JP


        Comment

        • kingofkolt

          #5
          Re: PREG matching

          "kingofkolt " <jessepNOSPAM@c omcast.net> wrote in message
          news:QL0%c.1423 35$mD.61186@att bi_s02...[color=blue]
          > "Logical" <me@privacy.net > wrote in message
          >[/color]
          news:413c25c9$0 $31704$61c65585 @un-2park-reader-01.sydney.pipen etworks.com.au. ..[color=blue][color=green]
          > > I'm trying to match:
          > > <image=1,righ t>
          > > - or -
          > > <image=1>
          > >
          > > where the 1 and 'right' become parameters.
          > >
          > > So far I have:
          > > preg_match_all( '#<image=(.+?), (.+?)> | <image=([^>]+)#msi', $body,
          > > $image_tags);
          > >
          > > and
          > >
          > > $image_count = preg_match_all( '#<image=([^>]+)#msi', $body,[/color][/color]
          $image_tags);[color=blue][color=green]
          > >
          > > I'm trying to see if I can get them both into the one regexep, I assume
          > > I have to use an alternative | somewhere, just not sure where.
          > >
          > > Can anyone help?[/color]
          >
          > I could be wrong, but wouldn't it be easier (or anyways take less[/color]
          processing[color=blue]
          > time) to do a str_replace()? Ex:
          >
          > <?php
          > $params=explode ( "|",
          > str_replace(
          > array( "<image=", ",", ">" ), array( "", "|", "" ), $body
          > )
          > );
          >
          > print_r($params );
          > ?>
          >
          > - JP
          >[/color]

          My mistake. That logic would require that $body contains only the line
          <image=1,righ t> and nothing else before or after the tag. Please ignore
          me...

          - JP


          Comment

          Working...