Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
is_blank
Forum Commoner
Posts: 36 Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA
Post
by is_blank » Sun Jun 26, 2005 8:09 am
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!
Revan
Forum Commoner
Posts: 83 Joined: Fri Jul 02, 2004 12:37 am
Location: New Mexico, USA
Contact:
Post
by Revan » Sun Jun 26, 2005 9:19 am
Hmm, try img src(.*?)\.jpg .
is_blank
Forum Commoner
Posts: 36 Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA
Post
by is_blank » Sun Jun 26, 2005 9:35 am
Yeah, I did that. It returns:
Warning: eregi_replace(): REG_BADRPT
...although I don't quite understand what's wrong with that.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Sun Jun 26, 2005 9:36 am
Moved to regex
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Sun Jun 26, 2005 9:38 am
Use perl style regex.
Code: Select all
$blah = preg_replace('/img src="([^"]*)"/i','img src="picotd.jpg"',$origString);
is_blank
Forum Commoner
Posts: 36 Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA
Post
by is_blank » Sun Jun 26, 2005 9:54 am
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!
)
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Sun Jun 26, 2005 10:23 am
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>
*/
is_blank
Forum Commoner
Posts: 36 Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA
Post
by is_blank » Sun Jun 26, 2005 11:06 am
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?
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Sun Jun 26, 2005 11:22 am
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....
is_blank
Forum Commoner
Posts: 36 Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA
Post
by is_blank » Sun Jun 26, 2005 12:09 pm
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!
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Sun Jun 26, 2005 12:19 pm
Nice website
Did you say you're a teacher/tutor? just curious what age group you teach
is_blank
Forum Commoner
Posts: 36 Joined: Sat Jun 25, 2005 6:05 pm
Location: Tennessee, USA
Post
by is_blank » Sun Jun 26, 2005 6:50 pm
High school--Sophomores, usually.
(Whoops, sorry; just caught the "UK"
10th grade, 15-16 year-olds.)