ira19 Posted September 1, 2006 Share Posted September 1, 2006 Hello, Can anyone please tell me how to extract the url from an anchor tag?Eg:<a href="www.url.com/index.html">HTML Code </a>I want www.url.com/index.html as well as HTML code....Please help!!! Link to comment https://forums.phpfreaks.com/topic/19332-extract-url-from-anchor-tag/ Share on other sites More sharing options...
ToonMariner Posted September 1, 2006 Share Posted September 1, 2006 $tag = '<a href="www.url.com/index.html">';$url = ereg_replace('/<a href="(?!")">/','\1',$tag); Link to comment https://forums.phpfreaks.com/topic/19332-extract-url-from-anchor-tag/#findComment-83961 Share on other sites More sharing options...
ira19 Posted September 1, 2006 Author Share Posted September 1, 2006 I get an error for $tag = '<a href="www.url.com/index.html">';$url = ereg_replace('/<a href="(?!\")">/','\1',$tag);besides the format can be either <a href="www.something.com/index.php">or <a href='www.something.com'> Link to comment https://forums.phpfreaks.com/topic/19332-extract-url-from-anchor-tag/#findComment-83989 Share on other sites More sharing options...
obsidian Posted September 1, 2006 Share Posted September 1, 2006 try this:[code]<?php$string = "<a href=\"www.url.com/index.html\">My URL</a>\n";preg_match('|<a.+?href\="(.+?)".*?>(.+?)</a>|i', $string, $match);$url = $match[1];$text = $match[2];?>[/code]just tried it, and it seems to work well. Link to comment https://forums.phpfreaks.com/topic/19332-extract-url-from-anchor-tag/#findComment-83993 Share on other sites More sharing options...
effigy Posted September 1, 2006 Share Posted September 1, 2006 [quote author=ToonMariner link=topic=106459.msg425873#msg425873 date=1157110986]$tag = '<a href="www.url.com/index.html">';$url = ereg_replace('/<a href="(?!")">/','\1',$tag);[/quote]ereg does not support negative lookaheads, and even if you go to preg, the expression with not work. You're telling it to find a double quote that is not followed by a double quote, that is followed by a double quote: an impossibility. Link to comment https://forums.phpfreaks.com/topic/19332-extract-url-from-anchor-tag/#findComment-84023 Share on other sites More sharing options...
ira19 Posted September 5, 2006 Author Share Posted September 5, 2006 Thanks for the help but let me explain in details what i exactly need. I want to search say www.domainname.com in other sites say www.ab.com,www.cc.com When i find that the domainname is present i want to retrieve the link as well as the texti.e <a href="http://www.domainname.com">My domain</a>the anchor text may also be like<a href="http://www.domainname.com" target="_blank">My domain</a>or<a href="http://www.domainname.com"><font size="2">My domain</font></a>and the output should be [b]www.domainname.com [/b]and [b]My domain[/b].Please help!!! Link to comment https://forums.phpfreaks.com/topic/19332-extract-url-from-anchor-tag/#findComment-86244 Share on other sites More sharing options...
rea|and Posted September 5, 2006 Share Posted September 5, 2006 Here's what I got about it: you want to have [B]< a href="an_absolute_path/optional/folder/file">an_absolute_path</ a> <strong>label_name</strong>[/B]from something like [B]< a href="an_absolute_path/optional/folder/file"><span>label_name</span></ a>[/B][CODE]$rex = '/(<a.+?href="https?:\/\/([^"]+?)(?:\/[^"]+)*".*?>)(.+?)<\/a>/iex' ;$rpl = '"$1$2</a> <strong>".strip_tags("$3")."</strong>"' ;$res = preg_replace( $rex,$rpl,$string ) ;[/CODE] But probably I didn't get it :) Link to comment https://forums.phpfreaks.com/topic/19332-extract-url-from-anchor-tag/#findComment-86321 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.