|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
||||
|
||||
|
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 |
|
#2
|
|||
|
|||
|
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? |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
|||
|
|||
|
You're welcome
![]() Replace both instances of Code:
[\"\'] with Code:
[\"\']? |
|
#5
|
||||
|
||||
|
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 |
|
#6
|
|||
|
|||
|
What does the regular expression look like now?
|
|
#7
|
||||
|
||||
|
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... |
|
#8
|
|||
|
|||
|
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. |
|
#9
|
||||
|
||||
|
Thank you, it's working now!
Cheers, Spongy |
![]() |
| Viewing: Dev Articles Community Forums > Web Design > Web Development > Regular Expressions, doesn't return wanted values... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|