SunQuest
 
           Web Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsWeb DesignWeb Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old March 15th, 2006, 09:43 AM
Spongy's Avatar
Spongy Spongy is offline
Alternately High
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Hilversum, Netherlands
Posts: 223 Spongy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 56 m 41 sec
Reputation Power: 5
Send a message via MSN to Spongy
Regular Expressions, doesn't return wanted values...

Hello,

I'm trying to create a regular expression for filtering out external urls out of anchor tags with PHP, but somehow it doesn't work properly. Maybe I have to use multiple expressions, but for some reason I have the idea this thing could be done by using just one. I created the following expression:
Code:
<a\s[^>]*href\s*=\s*[\"\'](http:\/\/[^>]*)[\"\']\s[^>]*>([^>]*)<\/a>

This expression does match with some urls, but only the ones with an extra attribute after the "href" (i.e. <a href="http://www.test.com" target="_new">Test</a> will return "http://www.test.com" and "Test". <a href="http://www.test.com">Test</a> will not match).

Does anyone here has any idea about this? I will be very thankful!!

Cheers,

Spongy
__________________
Work to live, don't live to work

Reply With Quote
  #2  
Old March 16th, 2006, 01:34 AM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 134 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 h 38 m 30 sec
Reputation Power: 4
It's doing what you told it to do. Remove the last '\s'.

Why did you post this here instead of in the PHP forum?

Reply With Quote
  #3  
Old March 16th, 2006, 06:03 AM
Spongy's Avatar
Spongy Spongy is offline
Alternately High
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Hilversum, Netherlands
Posts: 223 Spongy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 56 m 41 sec
Reputation Power: 5
Send a message via MSN to Spongy
Thanks for the help! I couldn't see the forest by the trees anymore.

But, next question... How can I extend this expression so that it'll match with bad coded anchors (i.e. <a href=www.google.com>google</a>) too, without the quotes?

Cheers,

Spongy

P.S.: The reason I posted here instead of the PHP forum, is that a previous question about regular expressions was moved here.

Last edited by Spongy : March 16th, 2006 at 06:05 AM.

Reply With Quote
  #4  
Old March 16th, 2006, 08:50 PM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 134 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 h 38 m 30 sec
Reputation Power: 4
You're welcome

Replace both instances of
Code:
[\"\']

with
Code:
[\"\']?

Reply With Quote
  #5  
Old March 18th, 2006, 10:29 AM
Spongy's Avatar
Spongy Spongy is offline
Alternately High
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Hilversum, Netherlands
Posts: 223 Spongy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 56 m 41 sec
Reputation Power: 5
Send a message via MSN to Spongy
Well, thanks for the input but it doesn't seem to work... It only returns 'http://' now... Do you have more suggestions?

Thanks in advance!

Cheers

Reply With Quote
  #6  
Old March 18th, 2006, 04:13 PM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 134 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 h 38 m 30 sec
Reputation Power: 4
What does the regular expression look like now?

Reply With Quote
  #7  
Old March 21st, 2006, 07:59 AM
Spongy's Avatar
Spongy Spongy is offline
Alternately High
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Hilversum, Netherlands
Posts: 223 Spongy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 56 m 41 sec
Reputation Power: 5
Send a message via MSN to Spongy
Well, with your suggestions it now looks like:
Code:
<a\s[^>]*href\s*=\s*[\"\']?(http:\/\/[^>]*)[\"\']?[^>]*>([^>]*)<\/a>


This expression just returns the 'http://' and not the complete url...

Reply With Quote
  #8  
Old March 24th, 2006, 09:54 PM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 134 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 h 38 m 30 sec
Reputation Power: 4
Try this.
Code:
/<a\s[^>]*href\s*=\s*[\"\']?(http:\/\/[^>\"\']*)[\"\']?[^>]*>([^>]*)<\/a>/i


If that doesn't work, then please show me the string you're testing with.

Reply With Quote
  #9  
Old March 28th, 2006, 08:10 AM
Spongy's Avatar
Spongy Spongy is offline
Alternately High
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Hilversum, Netherlands
Posts: 223 Spongy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 56 m 41 sec
Reputation Power: 5
Send a message via MSN to Spongy
Thank you, it's working now!

Cheers,

Spongy

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsWeb DesignWeb Development > Regular Expressions, doesn't return wanted values...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway