deane034 Posted February 17, 2010 Share Posted February 17, 2010 Hi, I'm developing a wordpress theme, and I want to extract all the HTML Links (hrefs) with the extension of MP3 into my side bar. I know the wordpress functions to get posts, etc. What I want to know is how to use PHP regular expressions to get a link for an .mp3 that exist as a link from a string variable. For example suppose the string is, $mystring= "<p> Hello this is a WP post. <a href="somefile.mp3">Here is a link to mp3<a/> </p> "; I want to be able to have a function which returns "somefile.mp3" I'm a bit of a noob when it comes to PHP regular expressions, Any help would be much appreciated. Thanks, Deane. Quote Link to comment Share on other sites More sharing options...
premiso Posted February 17, 2010 Share Posted February 17, 2010 May not be the best way, but will match it as long as there is only 1 mp3 link per line. preg_match('~href=[\'|"](.*\.mp3)[\'|"]~i', $mystring, $match); Quote Link to comment Share on other sites More sharing options...
cags Posted February 17, 2010 Share Posted February 17, 2010 Using the alternation bar inside a character class will simply allow the 'bar' character to match in the pattern, I think you were either thinking of using a capture group with an alternation or a character class. I'd personally opt for something along the lines of... ~href=([\'"])([^\1 ]+\.mp3)\1~i It uses back referencing to make sure the string delimiters of the href match. It will also cut out a bit sooner on hrefs that don't match since it will only match until it finds a space or the opening delimiter again. I believe technically an attribute doesn't require delimiters if it doesn't have spaces in it (which a URL obviously won't have), but I'm not entirely sure how you could accurately add that to the pattern, most people avoid that system anyway, so hopefully it's not a requirement. Quote Link to comment Share on other sites More sharing options...
cags Posted February 17, 2010 Share Posted February 17, 2010 Thanks to salathe for pointing out (via IRC) my, erm... deliberate mistake. That's what you get for posting before your morning coffee. A back reference can't be used in a character class. The \1 inside the character class actually represents the ascii character with the octal number 1 (SOH a control character), based on that it would perhaps make as much sense to simply use... ~href=([\'"])(\S+\.mp3)\1~i Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.