sphinx Posted April 3, 2013 Share Posted April 3, 2013 Hello, I'm looking to preg_match a link from another website, I want to match it because the actual link changes at random times, so I want to header redirect the returned link so it works regardless of any changes, In the source of the website, the block I need to copy is: <a style="margin: 5px 5px 5px 5px; background: url(/images/buttons/continue2.png) no-repeat; display: block; text-indent: -40000px; width: 110px; height: 37px;" href="i need to copy the link found in this box" target="_blank" >Continue</a> This area of the source seems to be unique so it wouldn't collide with any other code. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/276469-preg_match-a-link/ Share on other sites More sharing options...
remenissions Posted April 3, 2013 Share Posted April 3, 2013 Here's something I used to use longgggg ago if you want it, its not preg_match but it gets the job done. function get_string_between($string, $start, $end) { $string = " ".$string; $ini = strpos($string,$start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini; return substr($string,$ini,$len); } $curl= curl_init(); curl_setopt($curl, CURLOPT_URL, '(PUT YOUR LINK HERE)'; curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($curl); $Info = get_string_between($data, '<span>', '</span>'); Quote Link to comment https://forums.phpfreaks.com/topic/276469-preg_match-a-link/#findComment-1422617 Share on other sites More sharing options...
requinix Posted April 3, 2013 Share Posted April 3, 2013 Load up the HTML in DOMDocument, then do a getElementsByTagName() to find that link. If you know the URL will always follow some structure then you can "find that link" by checking all the hrefs until you get the right one. Otherwise you can look for a target=_blank attribute and inner HTML of "Continue". Both methods can break if they change the site enough. So... what's this for? It's odd that they're changing the link you need so often. Quote Link to comment https://forums.phpfreaks.com/topic/276469-preg_match-a-link/#findComment-1422708 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.