tags:

views:

43

answers:

3

Ive looked around quite a bit for a regex to help me find a anchor tag with an mp3 as the href. I would like first to get a regex which will locate this pattern so i can replace those links with a player and next i need to extract the link out of the href.

I hardly understand how to create or use a regex so any help to accomplish this will be useful.

Thanks, Jesse

A: 

This will do the trick

~<a.*?href="(?P<url>.*?\.mp3(?:\?.*?)?)".*?>~is

however, it's hardly to be considered optimal.

Mikulas Dite
what do you mean by its hardly optimal, is there a better way?
Jesse
A: 

at first this didnt quite work for me. I made a couple of changes and now I got it. i needed to change the ~ to a / and i had to replace all the way to the closing anchor tag so heres what im using.

/<a.*?href="(?P<url>.*?\.mp3(?:\?.*?)?)">.*?<\/a>/is

Thanks for getting me in the right direction

Jesse
+1  A: 

Supposing you already got the links —e.g. get_elements_by_tagname('a')— all you need to do is:

if( substr($href, -4)=='.mp3' ){
    // Is an MP3 link
}
Álvaro G. Vicario