List Info

Thread: Shortening Long Links in User Posts




Shortening Long Links in User Posts
country flaguser name
United States
2007-02-02 15:48:39
I'm wondering the best way to take any given blog post,
search for
links, and replace them with shortened versions (showing
part of the
link, and shortening the rest with "...").  E.g.
finding something
like

<a href="http://www.sldfkjkdf.com/asdflkjsdjf/aasdlfjlaksdfa
sdf/asdfd?
asdfjfjl=sfasdfasd&asdfasdfasdfasdfdf">http://www.sldfkjkdf.com/
asdflkjsdjf/aasdlfjlaksdfasdf/asdfd?
asdfjfjl=sfasdfasd&asdfasdfasdfasdfdf</a>

and shortening this to

<a href="http://www.sldfkjkdf.com/asdflkjsdjf/aasdlfjlaksdfa
sdf/asdfd?
asdfjfjl=sfasdfasd&asdfasdfasdfasdfdf">http://www.sldfkjkdf.com/
asdfdf...</a>

I'm using PHP.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://grou
ps-beta.google.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: Shortening Long Links in User Posts
country flaguser name
United States
2007-02-08 10:58:09
This one should work for most instances:

'/(?<!(href|HREF)=")(?P<displaytext>\b(?P<
protocol>https?|ftp)://(?
P<domain>[-A-Za-z0-9.]+))(?P<file>/[-A-Za-z0-9+
&#/%=~_|!:,.;]*)?(?
P<parameters>\?[-A-Za-z0-9+&#/%=~_|!:,.;]*)?/'

It'll skip of the text inside the anchor tag, and capture
the what you
want to keep in the back reference "displaytext".

It's based on a one of the stock regexes from my regex buddy
library
so it won't be too bad, but I'm still a newbie to regex so
it'll
probably need to be optimized.



joelbyrd wrote:
> I'm wondering the best way to take any given blog post,
search for
> links, and replace them with shortened versions
(showing part of the
> link, and shortening the rest with "..."). 
E.g. finding something
> like
>
> <a href="http://www.sldfkjkdf.com/asdflkjsdjf/aasdlfjlaksdfa
sdf/asdfd?
> asdfjfjl=sfasdfasd&asdfasdfasdfasdfdf">http://www.sldfkjkdf.com/
> asdflkjsdjf/aasdlfjlaksdfasdf/asdfd?
> asdfjfjl=sfasdfasd&asdfasdfasdfasdfdf</a>
>
> and shortening this to
>
> <a href="http://www.sldfkjkdf.com/asdflkjsdjf/aasdlfjlaksdfa
sdf/asdfd?
> asdfjfjl=sfasdfasd&asdfasdfasdfasdfdf">http://www.sldfkjkdf.com/
> asdfdf...</a>
>
> I'm using PHP.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://grou
ps-beta.google.com/group/regex
-~----------~----~----~----~------~----~------~--~---


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )