Web Design Forums

Other Languages

Get help with any programming languages other than the ones above.

Regex to return <a href> attributes



Site of the Month Voting - Now Open. CAST YOUR VOTE NOW!

Reply
 
LinkBack Thread Tools
Old July 29 '08, 06:43 AM (#1)
bfsog is offline
Coder
 
bfsog's Avatar
 
Join Date: May 2003
Location: UK
Posts: 2,354
bfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to behold
Send a message via MSN to bfsog
Regex to return <a href> attributes

I can throw together some regular expressions that extract dates and other easy stuff but this is pretty taxing, for me anway.

Lets say I have the following HTML

HTML Code:
<ul>
	<li><a href="file1.html" title="This is a title for file1">file 1</a></li>
	<li><a href="banana.html" title="">Banana</a></li>
	<li><a href="tg.html">tg</a></li>
	<li><a href="bfsog.html" id="bfsog" title="biffy">biffy</a></li>
</ul>
I want the href and the link text. I have come up with
Code:
([<a href=\".*\">.*</a>])
To be honest I am not sure if that actually works, I have been ducking between doing this in PHP and C# (A php RE will not work in a C# app I do not think).

So in short, from the above example I would like something like

Quote:
file1.html file1
banana.html Banana
tg.html tg
bfsog.html biffy
Thanks in advance
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old July 29 '08, 09:25 AM (#2)
filburt1 is offline
bored
 
filburt1's Avatar
 
Join Date: July 2002
Location: Maryland, US
Posts: 11,785
filburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to all
<a.*?href\s*=\s*["'](.*?)['"].*?>(.*?)</a>

Untested but I think will work.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old July 29 '08, 09:53 AM (#3)
bfsog is offline
Coder
 
bfsog's Avatar
 
Join Date: May 2003
Location: UK
Posts: 2,354
bfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to behold
Send a message via MSN to bfsog
It probably is me more than your code, but I cannot seem to get that to work, parse errors and what not.

It's annoying though, in a C# app I can extract the href="" value but the RE that I use cannot be used in PHP, and vice versa.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old July 29 '08, 10:48 AM (#4)
filburt1 is offline
bored
 
filburt1's Avatar
 
Join Date: July 2002
Location: Maryland, US
Posts: 11,785
filburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to all
The error specifically?

If you're using it inside preg_match, prefix it with / and postfix it with /i
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old July 29 '08, 10:58 AM (#5)
bfsog is offline
Coder
 
bfsog's Avatar
 
Join Date: May 2003
Location: UK
Posts: 2,354
bfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to behold
Send a message via MSN to bfsog
Using inside preg_match_all.

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

However I am fed up now, I think I shall invest in some RE books.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old July 29 '08, 11:17 AM (#6)
filburt1 is offline
bored
 
filburt1's Avatar
 
Join Date: July 2002
Location: Maryland, US
Posts: 11,785
filburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to all
Oh, well escape either the ' or the ", depending on what quotes you're using to surround the regex itself.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

  Web Design Forums » Programming Help » Other Languages

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JS menu alignment in IE robbieM Javascript, AJAX, and JSON 0 January 28 '05 10:33 AM

 
User Infomation
Your Avatar

Site Of The Month
Nominate Your Site Now!

Advertisement
WolfCMS.org

Latest Articles
- by RickM
- by bfsog

Advertisement

Partner Links



All times are GMT -4. The time now is 02:05 PM.


WebDesignForums.net is Copyright © 2010 RikeMedia.

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164