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 February 25th, 2013, 11:34 AM
pooleleven pooleleven is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 pooleleven User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 25 sec
Reputation Power: 0
PHP - Manipulate the text-part of a link

Hallo Forum,

in a PHP-Project I manipulate a simple link with:

PHP Code:
function replace_sulr($str) { 
$pattern '#(^|[^\"=]{1})(http://|ftp://|mailto:|news:)([^\s<>]+)([\s\n<>]|$)#sm'
return 
preg_replace($pattern,"\\1<a href=\"\\2\\3\" target=\"_blank\" class=\"crmnewsdetail_link\"><span class=\"crmnewsdetail_linkq\">&nbsp;</span>\\3</a>\\4",$str);
 } 


... and ist working.
But Part 3 is sometimes too long. I would like to shorten the "linkname" if it is longer than 50 letters. And I would like to shorten it from the middle:

wxw/en/snowboarding/episodes/1331581260962/chamaleon-the-project-begins
should become to:
xwx/en/sn...hamaleon-the-project-begins

(wxw for w..w because of the rules)

Is it possible to integrate this to a regex-pattern like this I'm using until now.

Thanks for your help!

Reply With Quote
  #2  
Old February 25th, 2013, 12:00 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,682 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 2 h 35 m 32 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
Use preg_replace_callback() and make the callback function shorten $3 before returning the new string. Like
PHP Code:
function replace_sulr($str) {
    
$pattern '#(^|[^\"=]{1})(http://|ftp://|mailto:|news:)([^\s<>]+)([\s\n<>]|$)#sm';
    return 
preg_replace_callback($pattern, function($match) {
        
$format "%s<a href=\"%s%s\" target=\"_blank\" class=\"crmnewsdetail_link\"><span class=\"crmnewdetail_linq\">&nbsp;</span>%s</a>%s";
        list(, 
$before$protocol$url$after) = $match;
        if (
strlen($url) > 50) {
            
$shorturl substr($url010) . "..." substr($url, -20);
        } else {
            
$shorturl $url;
        }
        return 
sprintf($format$before$protocol$url$shorturl$after);
    }, 
$str);


Reply With Quote
  #3  
Old February 25th, 2013, 12:17 PM
pooleleven pooleleven is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 pooleleven User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 25 sec
Reputation Power: 0
Thanks ... but first I must work me throught this stuff ;-)

Reply With Quote
  #4  
Old February 25th, 2013, 12:34 PM
pooleleven pooleleven is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 pooleleven User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 25 sec
Reputation Power: 0
Perfect ... Thanks

I didn't know about preg_replace_callback. Never used bevor.
Nice idea! And thanks again!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > PHP - Manipulate the text-part of a link

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