Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 30th, 2009, 04:46 AM
andyg1 andyg1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 235 andyg1 User rank is Corporal (100 - 500 Reputation Level)andyg1 User rank is Corporal (100 - 500 Reputation Level)andyg1 User rank is Corporal (100 - 500 Reputation Level)andyg1 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 7 h 12 m 32 sec
Reputation Power: 14
Identify external links in a string and add text to them

Guys and girls,

I'm using PHP. I usually like to work these things out myself but I'm totally stuck.

Taking a clump of text e.g. from a CMS entry, I want to check all the links in the text and for those that do not contain

"folder1/folder2"

in the URL path I want to add the text along the lines

"(this link will take you away from this section)"

to the text in the anchor tag, for all links in the text provided.

E.g.
Code:
<a href="some/other/place">my link</a>

would become
Code:
<a href="some/other/place">my link (this link will take you away from this section)</a>


I got as far as this for the expression:
PHP Code:
 preg_match('/<a.+href=".*folder1/folder2.*".*>.+</a>/i'$testString

but couldn't even get that to work...

Really appreciate some help with this. Many thanks.
__________________
Time isn't wasted if you're wasted all the time

Reply With Quote
  #2  
Old January 30th, 2009, 11:56 AM
requinix's Avatar
requinix requinix is online now
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,698 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 54 m 57 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
PHP Code:
 preg_replace('#(<a href="((?!folder1/folder2)[^"])+"[^>]*>)(.*?)</a>#is''$1$3 (this link will take you away from this section)</a>'$text); 

If you need that explained (it's okay if you do) just say something. I would now but I just woke up and I feel weird. Not sick, just... weird. I could probably give a good explanation now but I think it'd be best if I waited a bit before trying to talk at great lengths about something kinda complicated.

[edit] It's amazing what food can do to you. Except for the Raisin Bran aftertaste I feel pretty good.
Code:
(<a href="((?!folder1/folder2)[^"])+"[^>]*>)(.*?)</a>

The important part is the ((?!folder1/folder2)[^"])+. (?!...) means that there should not be a ... at this point. Put it before a [^"] and you get the next character isn't a " and neither is it the start of .... Now that only takes one character, so it gets grouped together with a () and repeated with a +.
(There are a couple other ways of doing the same thing but that's the method I prefer.)

The rest is simple if you know that .*? is the same as .* except it matches as few characters as possible.

Last edited by requinix : January 30th, 2009 at 01:48 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Identify external links in a string and add text to them

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap