Regexp for SEO purpose - img html alt tag

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

Moderator: General Moderators

Post Reply
AudiS2
Forum Newbie
Posts: 5
Joined: Thu Feb 28, 2008 6:02 pm

Regexp for SEO purpose - img html alt tag

Post by AudiS2 »

Hi Gang,

I am trying to create a php function that will parse a html, and alter or <img> tags to add the alt and title attributes that will match the src filename.

If you do not know html this is what I am trying to do.

Code: Select all

<img src="http://www.someserver.com/.../ferrari.jpg">
should be replaced with

Code: Select all

<img src="http://www.someserver.com/.../ferrari.jpg" alt="ferrari" title="ferrari" >
If you understand html you will know what I mean.

So function should work like this

1. Accept a string containing html with zero or more <img> tags
2. Extract the name of the image from the src attribute. src can be found in several forms:
src="http:/.../image.ext"
src = "http:/.../image.ext"
src ='http:/.../image.ext'
src=http:/.../image.ext
3. Add alt="image" and title="image" (without extension) to the <img> tag
4. Repeat for all images
5. Return altered html (yikes!)

Any gurus out there?
AudiS2
Forum Newbie
Posts: 5
Joined: Thu Feb 28, 2008 6:02 pm

Re: Regexp for SEO purpose - img html alt tag

Post by AudiS2 »

I forgot to add that sometimes alt or title tags can already exist and in that case no change is made for them.

Anyone for a quick regexp?
Post Reply