Jump to content

regex, detecting specific urls, help please


cjm771

Recommended Posts

I used this function to grab [b]link[/b] and the[b] text[/b] the url surrounds from <a href=''></a> html code in a given html page. Now I want to edit the function but restrict the links to mp3, wav, and sound files only if mp3search==true. My code right now finds the mp3s [b]link[/b] but it[u] doesnt [/u]find the right [b]text [/b](stuff between the href tags). Here is the function I used:


[code]function remove_html(&$item, $key)
{
  $item=trim(strip_tags($item));
}

function get_links($url, $mp3search) {
if ($mp3search==false)
$preg =
"/a[\s]+[^>]*?class=l[\s]+[^>]*?href[\s]?=[\s\"\']+(.*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/";
else
$preg =
"/a[\s]+[^>]*?href[\s]?=[\s\"\']+(.*?(mp3|wav))[\"\']+.*?>"."([^<]+|.*?)?<\/a>/i";
  preg_match_all(trim($preg),
          file_get_contents($url), $out, PREG_PATTERN_ORDER);
  $keys = $out[1];
  $values = $out[2];
  array_walk($values, 'remove_html');
  return (array_combine_emulated($keys, $values));
}
[/code]

p.s. array_combine_emulated() emulates array_combine (PHP5)
Link to comment
Share on other sites

I'm just going to start from scratch here, as I'm not sure what all you're doing. The following will match the href and the text of a link:
[code]
/<a .+?href=("|')([^\1]+?)\1.*?[^>]?>([^<]+?)<\/a>/
[/code]

The second and third backreferences hold your URL and text. If you want to limit it to .mp3 and .wav file, just modify it like this:
[code]
/<a .+?href=("|')([^\1]_?(\.mp3|\.wav))\1.*?[^>]?>([^<]+?)<\/a>/
[/code]

Obviously, you'll need to escape some quotes in there once you put it into code, but it should give you something to go off of.

Good luck
Link to comment
Share on other sites

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.