By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,900 Members | 1,337 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,900 IT Pros & Developers. It's quick & easy.

eregi and eregi replace

P: n/a
Nel
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('(=")(pics/)([0-9a-zA-Z%/ ]+.[a-zA-z]..)(")',$htmlsource,$imagesintext);
?>

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.
Jan 11 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Nel wrote:
<?PHP
$htmlsource = '<img src="pics/hotdog.gif"> text text <img
src="pics/silly%20sausage.gif"> ';
eregi('(=")(pics/)([0-9a-zA-Z%/ ]+.[a-zA-z]..)(")',$htmlsource,$imagesintext);
?>
[...]
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???
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.

http://www.php.net/manual/en/functio...-match-all.php
Also how to use replace to remove the pics/ bit of the string.


Replace them with the zero string. preg_replace().

http://www.php.net/manual/en/function.preg-replace.php

--
Jock

Jan 11 '06 #2

P: n/a

"John Dunlop" <us*********@john.dunlop.name> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Nel wrote:
<?PHP
$htmlsource = '<img src="pics/hotdog.gif"> text text <img
src="pics/silly%20sausage.gif"> ';
eregi('(=")(pics/)([0-9a-zA-Z%/ ]+.[a-zA-z]..)(")',$htmlsource,$imagesintext);
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('(=")(pics/)([0-9a-zA-Z_\-%/ ]+\.[jpgifpnJPGIFPN]{3})(")',$htmlsource,$imagesintext);
BTW, you should not allow spaces (%20) in filenames on a web site.
?>
[...]
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???


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.

Try to tinker with preg_grep?


http://www.php.net/manual/en/functio...-match-all.php
Also how to use replace to remove the pics/ bit of the string.


Replace them with the zero string. preg_replace().

http://www.php.net/manual/en/function.preg-replace.php

--
Jock

Feb 8 '06 #3

P: n/a

"Jim Michaels" <jm******@nospam.yahoo.com> wrote in message
news:27******************************@comcast.com. ..

"John Dunlop" <us*********@john.dunlop.name> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Nel wrote:
<?PHP
$htmlsource = '<img src="pics/hotdog.gif"> text text <img
src="pics/silly%20sausage.gif"> ';
eregi('(=")(pics/)([0-9a-zA-Z%/ ]+.[a-zA-z]..)(")',$htmlsource,$imagesintext);
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('(=")(pics/)([0-9a-zA-Z_\-%/ ]+\.[jpgifpnJPGIFPN]{3})(")',$htmlsource,$imagesintext);
can be further shortened:
eregi('(=")(pics/)([\d\w_\-%/ ]+\.[jpgifpnJPGIFPN]{3})(")',$htmlsource,$imagesintext);

BTW, you should not allow spaces (%20) in filenames on a web site.
?>


[...]
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???


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.

Try to tinker with preg_grep?


http://www.php.net/manual/en/functio...-match-all.php
Also how to use replace to remove the pics/ bit of the string.


Replace them with the zero string. preg_replace().

http://www.php.net/manual/en/function.preg-replace.php

--
Jock


Feb 14 '06 #4

P: n/a
On 2006-02-14, Jim Michaels <jm******@nospam.yahoo.com> wrote:

"Jim Michaels" <jm******@nospam.yahoo.com> wrote in message
news:27******************************@comcast.com. ..

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('(=")(pics/)([0-9a-zA-Z_\-%/ ]+\.[jpgifpnJPGIFPN]{3})(")',$htmlsource,$imagesintext);


can be further shortened:
eregi('(=")(pics/)([\d\w_\-%/ ]+\.[jpgifpnJPGIFPN]{3})(")',$htmlsource,$imagesintext);


yeah, both are equally non-functional.
I think what Jim intended was

eregi('(=")(pics/)([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
Feb 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.