eregi and eregi replace

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

    eregi and eregi replace

    Hi all,

    I am struggling with understanding a small eregi problem in php4.

    My code:
    <?PHP
    $htmlsource = '<img src="pics/hotdog.gif"> text text <img
    src="pics/silly%20sausage .gif"> ';
    eregi('(=")(pic s/)([0-9a-zA-Z%/ ]+.[a-zA-z]..)(")',$htmlso urce,$imagesint ext);
    ?>

    var_dump gives this, so I know it's working
    Outputs
    array(5) {
    [0]=>
    string(18) "="pics/hotdog.gif""
    [1]=>
    string(2) "=""
    [2]=>
    string(5) "pics/"
    [3]=>
    string(10) "hotdog.gif "
    [4]=>
    string(1) """
    }

    What I am trying to do is replace all images in the format ="pics/image.jpg"
    with "image.jpg" and at the same time make a list of the files image.jpg.
    The problem is how to pick up ALL the occurances, not just the first???

    Also how to use replace to remove the pics/ bit of the string.

    Thanks,

    Nel.


  • John Dunlop

    #2
    Re: eregi and eregi replace

    Nel wrote:
    [color=blue]
    > <?PHP
    > $htmlsource = '<img src="pics/hotdog.gif"> text text <img
    > src="pics/silly%20sausage .gif"> ';
    > eregi('(=")(pic s/)([0-9a-zA-Z%/ ]+.[a-zA-z]..)(")',$htmlso urce,$imagesint ext);
    > ?>[/color]

    [...]
    [color=blue]
    > What I am trying to do is replace all images in the format ="pics/image.jpg"
    > with "image.jpg" and at the same time make a list of the files image.jpg.
    > The problem is how to pick up ALL the occurances, not just the first???[/color]

    preg_match_all( ). preg_match is described as an alternative to ereg()
    and it stops searching after finding one match, so I assume ereg()
    stops then too.


    [color=blue]
    > Also how to use replace to remove the pics/ bit of the string.[/color]

    Replace them with the zero string. preg_replace().



    --
    Jock

    Comment

    • Jim Michaels

      #3
      Re: eregi and eregi replace


      "John Dunlop" <usenet+2004@jo hn.dunlop.name> wrote in message
      news:1137011110 .230535.227620@ g49g2000cwa.goo glegroups.com.. .[color=blue]
      > Nel wrote:
      >[color=green]
      >> <?PHP
      >> $htmlsource = '<img src="pics/hotdog.gif"> text text <img
      >> src="pics/silly%20sausage .gif"> ';
      >> eregi('(=")(pic s/)([0-9a-zA-Z%/ ]+.[a-zA-z]..)(")',$htmlso urce,$imagesint ext);[/color][/color]

      Be aware that . matches any character: escape it with a \ if you want a
      dot.
      Also, the range A-z includes [\]^_` along the way. were you trying for this?
      eregi('(=")(pic s/)([0-9a-zA-Z_\-%/ ]+\.[jpgifpnJPGIFPN]{3})(")',$htmls ource,$imagesin text);
      BTW, you should not allow spaces (%20) in filenames on a web site.
      [color=blue][color=green]
      >> ?>[/color]
      >
      > [...]
      >[color=green]
      >> What I am trying to do is replace all images in the format
      >> ="pics/image.jpg"
      >> with "image.jpg" and at the same time make a list of the files image.jpg.
      >> The problem is how to pick up ALL the occurances, not just the first???[/color]
      >
      > preg_match_all( ). preg_match is described as an alternative to ereg()
      > and it stops searching after finding one match, so I assume ereg()
      > stops then too.[/color]


      Try to tinker with preg_grep?

      [color=blue]
      >
      > http://www.php.net/manual/en/functio...-match-all.php
      >[color=green]
      >> Also how to use replace to remove the pics/ bit of the string.[/color]
      >
      > Replace them with the zero string. preg_replace().
      >
      > http://www.php.net/manual/en/function.preg-replace.php
      >
      > --
      > Jock
      >[/color]


      Comment

      • Jim Michaels

        #4
        Re: eregi and eregi replace


        "Jim Michaels" <jmichae3@nospa m.yahoo.com> wrote in message
        news:27-dnTtIL5L0NXTenZ 2dnUVZ_vmdnZ2d@ comcast.com...[color=blue]
        >
        > "John Dunlop" <usenet+2004@jo hn.dunlop.name> wrote in message
        > news:1137011110 .230535.227620@ g49g2000cwa.goo glegroups.com.. .[color=green]
        >> Nel wrote:
        >>[color=darkred]
        >>> <?PHP
        >>> $htmlsource = '<img src="pics/hotdog.gif"> text text <img
        >>> src="pics/silly%20sausage .gif"> ';
        >>> eregi('(=")(pic s/)([0-9a-zA-Z%/ ]+.[a-zA-z]..)(")',$htmlso urce,$imagesint ext);[/color][/color]
        >
        > Be aware that . matches any character: escape it with a \ if you want a
        > dot.
        > Also, the range A-z includes [\]^_` along the way. were you trying for
        > this?
        > eregi('(=")(pic s/)([0-9a-zA-Z_\-%/ ]+\.[jpgifpnJPGIFPN]{3})(")',$htmls ource,$imagesin text);[/color]

        can be further shortened:
        eregi('(=")(pic s/)([\d\w_\-%/ ]+\.[jpgifpnJPGIFPN]{3})(")',$htmls ource,$imagesin text);

        [color=blue]
        > BTW, you should not allow spaces (%20) in filenames on a web site.
        >[color=green][color=darkred]
        >>> ?>[/color]
        >>
        >> [...]
        >>[color=darkred]
        >>> What I am trying to do is replace all images in the format
        >>> ="pics/image.jpg"
        >>> with "image.jpg" and at the same time make a list of the files
        >>> image.jpg.
        >>> The problem is how to pick up ALL the occurances, not just the first???[/color]
        >>
        >> preg_match_all( ). preg_match is described as an alternative to ereg()
        >> and it stops searching after finding one match, so I assume ereg()
        >> stops then too.[/color]
        >
        >
        > Try to tinker with preg_grep?
        >
        >[color=green]
        >>
        >> http://www.php.net/manual/en/functio...-match-all.php
        >>[color=darkred]
        >>> Also how to use replace to remove the pics/ bit of the string.[/color]
        >>
        >> Replace them with the zero string. preg_replace().
        >>
        >> http://www.php.net/manual/en/function.preg-replace.php
        >>
        >> --
        >> Jock
        >>[/color]
        >
        >[/color]


        Comment

        • Jasen Betts

          #5
          Re: eregi and eregi replace

          On 2006-02-14, Jim Michaels <jmichae3@nospa m.yahoo.com> wrote:[color=blue]
          >
          > "Jim Michaels" <jmichae3@nospa m.yahoo.com> wrote in message
          > news:27-dnTtIL5L0NXTenZ 2dnUVZ_vmdnZ2d@ comcast.com...[color=green]
          >>
          >> Be aware that . matches any character: escape it with a \ if you want a
          >> dot.
          >> Also, the range A-z includes [\]^_` along the way. were you trying for
          >> this?
          >> eregi('(=")(pic s/)([0-9a-zA-Z_\-%/ ]+\.[jpgifpnJPGIFPN]{3})(")',$htmls ource,$imagesin text);[/color]
          >
          > can be further shortened:
          > eregi('(=")(pic s/)([\d\w_\-%/ ]+\.[jpgifpnJPGIFPN]{3})(")',$htmls ource,$imagesin text);[/color]

          yeah, both are equally non-functional.


          I think what Jim intended was

          eregi('(=")(pic s/)([0-9A-Z_/\ %-]+\.[fgijnp]{3})(")'
          ,$htmlsource
          ,$imagesintext) ;


          But thare's no law that images must have filenames that match that.

          This: <img src="randompic. 001.php">
          is valid if the PHP sets content-type and emits a correctly formed inage...


          Bye.
          Jasen

          Comment

          Working...