andre3 Posted October 25, 2008 Share Posted October 25, 2008 Hi guys is there away to get http://link.com from this block code with php? and assign it as a variable: <a href='http://link.com'>test</a> please demonstrate thanks! Link to comment https://forums.phpfreaks.com/topic/130087-is-it-possible-for-me-to-get/ Share on other sites More sharing options...
pspfreak101 Posted October 25, 2008 Share Posted October 25, 2008 what you mean something like this echo "<A HREF='http://link.com'>$varible</a>"; or $varible '<A HREF='http://link.com'>link</a>'; Link to comment https://forums.phpfreaks.com/topic/130087-is-it-possible-for-me-to-get/#findComment-674485 Share on other sites More sharing options...
ram4nd Posted October 25, 2008 Share Posted October 25, 2008 Umm dude he said variable and after edited you have a missing "="... Here are 2 examples: $string = '<a href="http://link.com">test</a>'; or $string = <<<CONTENT <a href="http://link.com">test</a> CONTENT; Link to comment https://forums.phpfreaks.com/topic/130087-is-it-possible-for-me-to-get/#findComment-674487 Share on other sites More sharing options...
genericnumber1 Posted October 25, 2008 Share Posted October 25, 2008 the simplest way would be with regex. There are more complicated patterns that can do this more efficiently, but you'll have to specify if you want it to be more loose (about whitespace, different types of quotes, ignoring \', etc). This pattern is just for showing you the concept. <?php $string = "<a href='http://link.com'>test</a> <a href='stuff.com'>somethingelse</a>"; $result = preg_match_all("%<a href='([^']*)'>%", $string, $matches); print_r($matches); ?> the result of that would be Array ( [0] => Array ( [0] => <a href='http://link.com'> [1] => <a href='stuff.com'> ) [1] => Array ( [0] => http://link.com [1] => stuff.com ) ) as you can see, you can get the link you want from $matches[1][0]. Link to comment https://forums.phpfreaks.com/topic/130087-is-it-possible-for-me-to-get/#findComment-674488 Share on other sites More sharing options...
andre3 Posted October 25, 2008 Author Share Posted October 25, 2008 hehe thanks alot bro for this code, one question, even if its a clickable image will it still get url from href tag right ? $result = preg_match_all("%<a href=\"([^']*)\">%", $ads, $matches); Link to comment https://forums.phpfreaks.com/topic/130087-is-it-possible-for-me-to-get/#findComment-674553 Share on other sites More sharing options...
genericnumber1 Posted October 25, 2008 Share Posted October 25, 2008 Well the reason I said it is just a concept pattern, is it will only match things pretty strictly... %<a href=\"([^"]*)\">% will only match links of the form <a href="something"> any little change such as an extra space (<a href=...) or a different type of quote (<a href='something'>) and it won't see it. You could change it to be less strict. eg... <?php $string = '<a class="stuff" href="http://link.com" id="candyapple">test</a> <a href=\'test\'>'; $result = preg_match_all('%<a[^>]*[\s]href=[\'"]([^\'"]*)[\'"][^>]*>%i', $string, $matches); print_r($matches); ?> and that will fix some of the the problems I mentioned above... it will match " or ' for quotes, ignore extra things in the a tag, etc.. But it still has some limitations... it still won't match those old htmlers who don't use quotes around their links (href=page.com) it still won't match someone who escapes a quote in their link (href='john\'s link') etc... it really comes down to how complicated you want your pattern to be. Link to comment https://forums.phpfreaks.com/topic/130087-is-it-possible-for-me-to-get/#findComment-674573 Share on other sites More sharing options...
andre3 Posted November 1, 2008 Author Share Posted November 1, 2008 i was ncountering some errors, but i used ur second suggestion and it works perfect, the way i did b4 was with alot of codes lol but now u cut that down to just 1 line thanks Link to comment https://forums.phpfreaks.com/topic/130087-is-it-possible-for-me-to-get/#findComment-680097 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.