Jump to content

PHP Regular expression to identify a link from html


Recommended Posts

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.

 

 

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.