techcone Posted August 7, 2008 Share Posted August 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/118690-regex-for-url-in-anchor-tag/ Share on other sites More sharing options...
Johntron Posted August 7, 2008 Share Posted August 7, 2008 I've been doing a lot of this lately /href=["'](/download/[^"']+)["']/ The parentheses capture the URL. Quote Link to comment https://forums.phpfreaks.com/topic/118690-regex-for-url-in-anchor-tag/#findComment-611093 Share on other sites More sharing options...
techcone Posted August 7, 2008 Author Share Posted August 7, 2008 Error encountered : Parse error: syntax error, unexpected ']' in C:\xampp\htdocs\rsddl\search.php on line 23 Quote Link to comment https://forums.phpfreaks.com/topic/118690-regex-for-url-in-anchor-tag/#findComment-611097 Share on other sites More sharing options...
Johntron Posted August 7, 2008 Share Posted August 7, 2008 You're not escaping the quotes. Paste your preg_match() code. Quote Link to comment https://forums.phpfreaks.com/topic/118690-regex-for-url-in-anchor-tag/#findComment-611100 Share on other sites More sharing options...
techcone Posted August 7, 2008 Author Share Posted August 7, 2008 This is the code i m using ! preg_match_all($urlpattern, $line, $matches); Quote Link to comment https://forums.phpfreaks.com/topic/118690-regex-for-url-in-anchor-tag/#findComment-611101 Share on other sites More sharing options...
Johntron Posted August 7, 2008 Share Posted August 7, 2008 preg_match_all('/href=["\'](/download/[^"\']+)["\']/', $line, $matches); Quote Link to comment https://forums.phpfreaks.com/topic/118690-regex-for-url-in-anchor-tag/#findComment-611104 Share on other sites More sharing options...
techcone Posted August 7, 2008 Author Share Posted August 7, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/118690-regex-for-url-in-anchor-tag/#findComment-611108 Share on other sites More sharing options...
nrg_alpha Posted August 7, 2008 Share Posted August 7, 2008 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[^"]+#' Quote Link to comment https://forums.phpfreaks.com/topic/118690-regex-for-url-in-anchor-tag/#findComment-611125 Share on other sites More sharing options...
sasa Posted August 12, 2008 Share Posted August 12, 2008 try preg_match_all('/href=["\'](\/download\/[^"\']+)["\']/', $line, $matches); echo "<pre>"; var_dump($matches); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/118690-regex-for-url-in-anchor-tag/#findComment-614627 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.