Page 1 of 1
A (very) little eregi_ help?
Posted: Sun Jun 26, 2005 8:09 am
by is_blank
I've got the following text snippet:
"some_image.jpg" will vary from time to time, but no matter what it is, I want to change
to
always read
img src="picotd.jpg"
I've tried this:
Code: Select all
$blah = eregi_replace('img src(.*)\.jpg"','img src="picotd.jpg"',$origString);
But it's too greedy; it matches:
I'm just finding my way around regular expressions, and I can't quite figure out how to make it quit at the
first ".jpg" instead of the second.
Any pointers? Thanks!
Posted: Sun Jun 26, 2005 9:19 am
by Revan
Hmm, try img src(.*?)\.jpg .
Posted: Sun Jun 26, 2005 9:35 am
by is_blank
Yeah, I did that. It returns:
Warning: eregi_replace(): REG_BADRPT
...although I don't quite understand what's wrong with that.

Posted: Sun Jun 26, 2005 9:36 am
by Chris Corbyn
Moved to regex

Posted: Sun Jun 26, 2005 9:38 am
by Chris Corbyn
Use perl style regex.
Code: Select all
$blah = preg_replace('/img src="([^"]*)"/i','img src="picotd.jpg"',$origString);

Posted: Sun Jun 26, 2005 9:54 am
by is_blank
d11wtq wrote:Use perl style regex.
Code: Select all
$blah = preg_replace('/img src="([^"]*)"/i','img src="picotd.jpg"',$origString);

That expression doesn't seem to change my string at all. (Could you explain what it's doing, while we're at it? I'd like to learn...

)
I figure I
could kludge it by catching a bit of the 'alt' parameter in there, to make it unique, i.e.:
Code: Select all
$blah = eregi_replace('img src(.*)\.jpg" alt','img src="picotdsm.jpg" alt alt',$origString);
to catch
and then just overlap the 'alt' in the replace string, but that seems
really sloppy...
(Sorry, didn't notice the 'regex' forum at first!

)
Posted: Sun Jun 26, 2005 10:23 am
by Chris Corbyn
Hmm... that should have worked prefectly. Could you post the actual code please?
I'll break it down.
1. Look for
img src="
2. Catch all characters but
not double quotes
3. Catch the final double quote.
4. Replace all of that with your new string.
Tested...
Code: Select all
$origString = '<a href="/link/to/some_image.jpg" class="image" title="Image Title"><img src="http://example.com/pretty/long/path/to/some_image.jpg" alt="Image Title" longdesc="/link/to/some_image.jpg" /></a>';
$blah = preg_replace('/img src="([^"]*)"/i','img src="picotd.jpg"',$origString);
echo $blah;
/*
<a href="/link/to/some_image.jpg" class="image" title="Image Title"><img src="picotd.jpg" alt="Image Title" longdesc="/link/to/some_image.jpg" /></a>
*/
Posted: Sun Jun 26, 2005 11:06 am
by is_blank
d11wtq wrote:Hmm... that should have worked prefectly. Could you post the actual code please?
Sure. The
entire code is long, and (no doubt!) a sloppy mess; here's the relevant snippet, though:
Code: Select all
$pic_chunk = '<a href="/wiki/Image:Bluesky2.jpg" class="image" title="Blue Sky"><img src=
"http://upload.wikimedia.org/wikipedia/en/thumb/d/dd/225px-Bluesky2.jpg" alt="Blue Sky" longdesc=
"/wiki/Image:Bluesky2.jpg" /></a>';
$final_image = preg_replace('/img src="([^"]*)"/i','img src="php/picotdsm.jpg"',$pic_chunk);
echo $final_image;
/*
<a href="/wiki/Image:Bluesky2.jpg" class="image" title="Blue Sky"><img src=
"http://upload.wikimedia.org/wikipedia/en/thumb/d/dd/225px-Bluesky2.jpg" alt="Blue Sky" longdesc=
"/wiki/Image:Bluesky2.jpg" /></a>
*/
Spot anything wrong?
Posted: Sun Jun 26, 2005 11:22 am
by Chris Corbyn
is_blank wrote:Spot anything wrong?
Yes

But not your fault. they have a linebreak in there just before the first quote...
Code: Select all
preg_replace('/img src=\s*"([^"]*)"/i','img src="php/picotdsm.jpg"',$pic_chunk);
Not sure if that's a copy/paste error though.
It works now however....
Posted: Sun Jun 26, 2005 12:09 pm
by is_blank
Beautiful. Thank you! It's good to not have to hack around!

If you'd like to see the result, it's the Picture of the Day in the sidebar on my front page (
http://www.ferrellweb.com--scroll waaaay down!) Thanks again for all the help!
Posted: Sun Jun 26, 2005 12:19 pm
by Chris Corbyn
Nice website
Did you say you're a teacher/tutor? just curious what age group you teach

Posted: Sun Jun 26, 2005 6:50 pm
by is_blank
High school--Sophomores, usually.

(Whoops, sorry; just caught the "UK"

10th grade, 15-16 year-olds.)