Alexk Posted October 13, 2007 Share Posted October 13, 2007 I have just started using php expressions. I am trying to make a script that returns all URLs from links. I thought that first of all, id have too remove the links code, such as: <a href = 'url'>urltext</a> this could then be placed in an array of links in a page. Then i would have too remove all of the HTML code around the URL. I have tried too do this myself, using: preg_split("/<a href = '(.*)'>(.*)<\/a>",$src,-1) however, it returns completely useless code (when i use print_r) Has anyone got any ideas so that i could get only the links code? Quote Link to comment https://forums.phpfreaks.com/topic/73104-solved-php-regular-expressions-help/ Share on other sites More sharing options...
MadTechie Posted October 14, 2007 Share Posted October 14, 2007 maybe this <?php preg_match_all('%<a href\s?=\s?(?:["|\']?)(.*?)(?:["|\']?)>(.*?)<\/a>%i', $src, $result, PREG_PATTERN_ORDER); $linkname= $result[1]; $URL = $result[2]; print_r($linkname); print_r($URL); ?> or to find any valid link (starting with http), try <?php preg_match_all('/\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]/i', $subject, $result, PREG_PATTERN_ORDER); $result = $result[0]; ?> please note we have a Regex within PHP section Quote Link to comment https://forums.phpfreaks.com/topic/73104-solved-php-regular-expressions-help/#findComment-368871 Share on other sites More sharing options...
nicephotog Posted October 14, 2007 Share Posted October 14, 2007 $filefound=""."\n"; # if thats properly behaving as perl then escape \< is required (php is dodgey on perl) # could need to shift +? or {4,*}? in or out of (.) note +? is the easier while(preg_match("/\<a href=(\"|\'|!\"|!\')(.{4,*}?)\<\/a>/",$inputString,$foundset)){ # single-first match $filefound.=$foundset[2]; # second subset } #.... print it to file Quote Link to comment https://forums.phpfreaks.com/topic/73104-solved-php-regular-expressions-help/#findComment-369217 Share on other sites More sharing options...
nicephotog Posted October 14, 2007 Share Posted October 14, 2007 $filefound=""."\n"; # if thats properly behaving as perl then escape \< is required (php is dodgey on perl) # could need to shift +? or {4,*}? in or out of (.) note +? is the easier while(preg_match("/\<a href=(\"|\'|!\"|!\')(.{4,*}?)\<\/a>/",$inputString,$foundset)){ # single-first match $filefound.=$foundset[2]; # second subset } #.... print it to file OOPS! SORRY THAT WAS A BIT QUICK!!! $filefound=""."\n"; $lg=length($inputString); $walk=0; # if thats properly behaving as perl then escape \< is required (php is dodgey on perl) # could need to shift +? or {4,*}? in or out of (.) note +? is the easier --first subset-- while(preg_match("/\<a href=(.{4,*}?) \<\/a>/",($inputString=substring($inputString,$walk)),$foundset)){ # single-first match $st=substr($foundset[1],0,1); $en=substr($foundset[1],-1); # trim ends if(preg_match("/(\"|\')/",$st){ $foundset[1]=substr($foundset[1],1); } if(preg_match("/(\"|\')/",$en){ $foundset[1]=substr($foundset[1],0,($lng=length($foundset[1])-1)); } $filefound.=$foundset[1]; $walk+=length($foundset[0])+1; } # enwhile #.... print it to file ###### oops! that was close Quote Link to comment https://forums.phpfreaks.com/topic/73104-solved-php-regular-expressions-help/#findComment-369228 Share on other sites More sharing options...
Alexk Posted October 16, 2007 Author Share Posted October 16, 2007 Thanks, that seems to have worked. Can anyone recommend any tutorials on expressions in PHP, and functions such as preg_replace, preg_match_all etc, apart from php.net? :) Quote Link to comment https://forums.phpfreaks.com/topic/73104-solved-php-regular-expressions-help/#findComment-370966 Share on other sites More sharing options...
MadTechie Posted October 16, 2007 Share Posted October 16, 2007 Please click solved (bottom left) personally i got the o'reilly Mastering regular expressions, Great book hard in parts! Also check our resource Quote Link to comment https://forums.phpfreaks.com/topic/73104-solved-php-regular-expressions-help/#findComment-370975 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.