A (very) little eregi_ help?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

A (very) little eregi_ help?

Post by is_blank »

I've got the following text snippet:
<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>
"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:
<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>
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!
Revan
Forum Commoner
Posts: 83
Joined: Fri Jul 02, 2004 12:37 am
Location: New Mexico, USA
Contact:

Post by Revan »

Hmm, try img src(.*?)\.jpg .
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Post 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.:?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Moved to regex :D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Use perl style regex.

Code: Select all

$blah = preg_replace('/img src="([^"]*)"/i','img src="picotd.jpg"',$origString);
;)
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Post 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... :wink:)

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
<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>
and then just overlap the 'alt' in the replace string, but that seems really sloppy...

(Sorry, didn't notice the 'regex' forum at first! :oops:)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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>
 */
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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....
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Post by is_blank »

Beautiful. Thank you! It's good to not have to hack around! :wink: 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!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Nice website ;)

Did you say you're a teacher/tutor? just curious what age group you teach :D
User avatar
is_blank
Forum Commoner
Posts: 36
Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA

Post by is_blank »

High school--Sophomores, usually. 8) (Whoops, sorry; just caught the "UK" :D 10th grade, 15-16 year-olds.)
Post Reply