Jump to content

Regex for url in anchor tag


techcone

Recommended Posts

Hi developers, once again I have a question.

 

There is a file which I fetch thru curl.

 

Now that file contains url in the form of

 

<a href="/download/121213/11211212.avi">

<a href="/download/1231213/1sdfadsf1211212.avi">

<a href="/download/12139765/11cxvae2.avi">

<a href="/download/123413/1121sdfsadf2.avi">

 

I want to extract such urls which begin from /download and not any other. I dont have idea of what regex to use. I know u guys can help me. Thanx in advance.

Link to comment
Share on other sites

preg_match_all('/href=["\'](/download/[^"\']+)["\']/', $line, $matches);
echo "<pre>";
var_dump($matches);
echo "</pre>";

 

Error :

 

Warning: preg_match_all() [function.preg-match-all]: Unknown modifier 'd' in C:\xampp\htdocs\rsddl\search.php on line 36

NULL

Link to comment
Share on other sites

Not sure if this is what you are looking for:

 

$str = '<a href="/download/121213/11211212.avi">';
$str = preg_match('#/download[^\"]+#' ,$str, $match);
echo $match[0];

 

Result of $match[0]:

/download/121213/11211212.avi

 

now if you want multiple results:

$str = array('<a href="/download/121213/11211212.avi">','<a href="/download/1231213/1sdfadsf1211212.avi">','<a href="/download/12139765/11cxvae2.avi">');
foreach($str as $val){
   preg_match_all('#/download[^\"]+#' , $val, $match, PREG_SET_ORDER);
   echo $match[0][0] . '<br />';
}

 

Results:

/download/121213/11211212.avi

/download/1231213/1sdfadsf1211212.avi

/download/12139765/11cxvae2.avi

 

Cheers,

 

NRG

 

P.S Now that I come to think of it, you may not need to use the escape slash in my regex exmaple.

so '#/download[^\"]+#' could be probably written as #/download[^"]+#'

 

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.