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
https://forums.phpfreaks.com/topic/118690-regex-for-url-in-anchor-tag/
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[^"]+#'

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.