Jump to content


Photo

extract url from anchor tag


  • Please log in to reply
6 replies to this topic

#1 ira19

ira19
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 01 September 2006 - 06:50 AM

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!!!

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 01 September 2006 - 11:43 AM

$tag = '<a href="www.url.com/index.html">';

$url = ereg_replace('/<a href="(?!")">/','\1',$tag);
follow me on twitter @PHPsycho

#3 ira19

ira19
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 01 September 2006 - 12:36 PM

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'>



#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 01 September 2006 - 12:41 PM

try this:
<?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];
?>

just tried it, and it seems to work well.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 01 September 2006 - 01:40 PM

$tag = '<a href="www.url.com/index.html">';
$url = ereg_replace('/<a href="(?!")">/','\1',$tag);


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.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#6 ira19

ira19
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 05 September 2006 - 10:23 AM

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 text
i.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.domainnam...name.com"><font size="2">My domain</font></a>
and the output should be www.domainname.com and My domain.

Please help!!!




#7 rea|and

rea|and
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 05 September 2006 - 12:47 PM

Here's what I got about it:
you want to have
    < a href="an_absolute_path/optional/folder/file">an_absolute_path</ a> <strong>label_name</strong>
from something like
      < a href="an_absolute_path/optional/folder/file"><span>label_name</span></ a>



$rex = '/(<a.+?href="https?:\/\/([^"]+?)(?:\/[^"]+)*".*?>)(.+?)<\/a>/iex' ;
$rpl = '"$1$2</a> <strong>".strip_tags("$3")."</strong>"' ;
$res =  preg_replace( $rex,$rpl,$string ) ;


But probably I didn't get it  :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users