birdie Posted March 1, 2006 Share Posted March 1, 2006 ok, i'm not completely sure how complicated this is but it is in my head.say i wanted to find a hyperlink url out of a normal php codeThe code that i would be designating would be something like..[code]<a href="thispage.php">Click here</a>[/code]How could i find "thispage.php"? ive tryed sub_str etc but i cant find a way to make them useful. any help? thanks Quote Link to comment Share on other sites More sharing options...
khburres Posted March 2, 2006 Share Posted March 2, 2006 Not exactly sure what you're looking for.Are you trying to extract the url from the html code?In that case, you could try something like that below.It uses regular expressions - though I must disclose I amfar from being an expert in the use of regular expressions.[code]<?php$html_code = '<a href="thispage.php">Click Here</a>';$pattern = '/(.*)(<a href=")(.*)(">)(.*)/';preg_match($pattern, $html_code, $matches);echo $matches[3];?>[/code] Quote Link to comment Share on other sites More sharing options...
birdie Posted March 2, 2006 Author Share Posted March 2, 2006 Thanks i think thats what i'm looking for. :-) Quote Link to comment Share on other sites More sharing options...
obsidian Posted March 2, 2006 Share Posted March 2, 2006 [!--quoteo(post=350978:date=Mar 2 2006, 10:10 AM:name=KHB)--][div class=\'quotetop\']QUOTE(KHB @ Mar 2 2006, 10:10 AM) [snapback]350978[/snapback][/div][div class=\'quotemain\'][!--quotec--]Not exactly sure what you're looking for.Are you trying to extract the url from the html code?In that case, you could try something like that below.It uses regular expressions - though I must disclose I amfar from being an expert in the use of regular expressions.[code]<?php$html_code = '<a href="thispage.php">Click Here</a>';$pattern = '/(.*)(<a href=")(.*)(">)(.*)/';preg_match($pattern, $html_code, $matches);echo $matches[3];?>[/code][/quote]great suggestion, but let me clean it up a tad... as it is, there are a lot of things that [i]could[/i] be in the source that may throw off your matches. i'd try to change it up just a tad:[code]$code = "<a href='http://www.cnn.com/'>CNN</a><br /><a href=\"google.com\">Google</a>\n";preg_match_all("|href=([\"'])(.+?)\\1|i", $code, $matches, PREG_PATTERN_ORDER);echo "URLs in this code:<br />\n";foreach ($matches[2] as $match) { echo "$match<br />\n";}[/code]this way, you account for hrefs within double OR single quotes, too... as well, you are accounting for the possibility of having multiple URLs in one string. Quote Link to comment Share on other sites More sharing options...
birdie Posted March 4, 2006 Author Share Posted March 4, 2006 [!--quoteo(post=351005:date=Mar 2 2006, 04:29 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Mar 2 2006, 04:29 PM) [snapback]351005[/snapback][/div][div class=\'quotemain\'][!--quotec--]great suggestion, but let me clean it up a tad... as it is, there are a lot of things that [i]could[/i] be in the source that may throw off your matches. i'd try to change it up just a tad:[code]$code = "<a href='http://www.cnn.com/'>CNN</a><br /><a href=\"google.com\">Google</a>\n";preg_match_all("|href=([\"'])(.+?)\\1|i", $code, $matches, PREG_PATTERN_ORDER);echo "URLs in this code:<br />\n";foreach ($matches[2] as $match) { echo "$match<br />\n";}[/code]this way, you account for hrefs within double OR single quotes, too... as well, you are accounting for the possibility of having multiple URLs in one string.[/quote]thanks thats even better! Quote Link to comment 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.