开发者

Help with PHP preg_replace() REGEX for MP3 URLs

开发者 https://www.devze.com 2023-01-05 20:31 出处:网络
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 pla

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 th开发者_运维问答e href.

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

Thanks, Jesse


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
}


This will do the trick

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

however, it's hardly to be considered optimal.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消