Solved

Concatenated URL's - RegEx

Posted on 2004-09-08
9
229 Views
Last Modified: 2008-02-20

$search = array('#\[url]([a-z0-9]+?://){1}([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)\[/url\]#', '#\[url]((www|ftp)\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\[/url\]#');
$replace = array('<a href="\1\2" target="_blank" title="\1\2">link</a>', '<a href="http://\1" target="_blank" title="\1">link</a>' );
$str = preg_replace($search, $replace, $str);

here's the current bbcode script for replacing [url]http://url_here[/url] with link

i wanna change it so it's it shows the link but if its past 'X' amount of characters it cuts out the middle of the link and replaces it with '...'

i.e.
ORIGINAL: http://www.experts-exchange.com/Web/Web_Languages/PHP/askQuestion.jsp
UPDATED: http://www.experts-exch...HP/askQuestion.jsp
0
Comment
Question by:chrisnetonline
  • 5
  • 4
9 Comments
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Hi

Can't tell, but you can try this: (probably not most effective though)

if ($l=strlen($1.$2)>30) {
   $checkEnd=explode("/",$1.$2);
   $f_len=strlen($checkEnd(count($checkEnd)-1))+3;
   $f_len=gmp_neg($f_len);
   $cut_link=substr($1.$2,0,23)."...".substr($1.$2,$f_len,($h-$f_len));
} else {
   $cut_link=$1.$2;
}
$replace = array('<a href="\1\2" target="_blank" title="\1\2">$cut_link</a>', '<a href="http://\1" target="_blank" title="\1">$cut_link</a>' );

Regards,
Zyloch
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Oops, this: substr($1.$2,$f_len,($h-$f_len)); should be
substr($1.$2,$f_len,($l-$f_len));

Regards
0
 

Author Comment

by:chrisnetonline
Comment Utility
how would this be implemented though? i dont see how that code is being run on the URL...
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Try putting it on the line after $search = blah but before $replace = blah
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:chrisnetonline
Comment Utility
okay so here's the code i have:

<?php

function run_test($str)
{
      $search = array('#\[url]([a-z0-9]+?://){1}([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)\[/url\]#', '#\[url]((www|ftp)\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\[/url\]#');
      
      if ($l=strlen($1.$2)>30) {
         $checkEnd=explode("/",$1.$2);
         $f_len=strlen($checkEnd(count($checkEnd)-1))+3;
         $f_len=gmp_neg($f_len);
         $cut_link=substr($1.$2,0,23)."...".substr($1.$2,$f_len,($l-$f_len));
      } else {
         $cut_link=$1.$2;
      }

      $replace = array('<a href="\1\2" target="_blank" title="\1\2">'.$cut_link.'</a>', '<a href="http://\1" target="_blank" title="\1">'.$cut_link.'</a>' );
      $str = preg_replace($search, $replace, $str);
      return $str;
}

echo run_test("[url]http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21124048.html[/url]");

?>

i get the following error:

Parse error: parse error, unexpected T_DNUMBER, expecting T_VARIABLE or '$' in /var/www/html/chris/test.php on line 7
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
Try changing this line:

$f_len=strlen($checkEnd(count($checkEnd)-1))+3; to
$f_len=strlen($checkEnd[count($checkEnd)-1])+3;
0
 

Author Comment

by:chrisnetonline
Comment Utility
still getting the following error:

Parse error: parse error, unexpected T_DNUMBER, expecting T_VARIABLE or '$' in /var/www/html/chris/test.php on line 7
0
 
LVL 36

Accepted Solution

by:
Zyloch earned 250 total points
Comment Utility
Er... you can try this

preg_match($search,$str);
$t=$1.$2;
if ($l=strlen($t)>30) {
        $checkEnd=explode("/",$t);
        $f_len=strlen($checkEnd[count($checkEnd)-1])+3;
        $f_len=gmp_neg($f_len);
        $cut_link=substr($t,0,23)."...".substr($t,$f_len,($l-$f_len));
} else {
        $cut_link=$t;
}
0
 

Author Comment

by:chrisnetonline
Comment Utility
Thank you Zyloch... your code lead me to the answer i was looking for... the completed code is included below


function shorten_url($matches)
{
      $str=$matches[1].$matches[2];
      $len=strlen($str);
      if($len > 50)
      {
            $checkEnd=explode('/', $str);
            $f_len=strlen($checkEnd[count($checkEnd)-1])+1;
            if($f_len == 1)
            {
                  $f_len=strlen($checkEnd[count($checkEnd)-2])+1;
            }
            elseif($f_len < 25)
            {
                  $f_len=(($f_len < 0)?($f_len * -1):($f_len * -1));
                  $cut_link=substr($str, 0, 25).'...'.substr($str, $f_len, ($len-$f_len));
            }
            else
            {
                  $cut_link=substr($str, 0, 25).'...'.substr($str, -25);
            }
      }
      else
      {
            $cut_link=$str;
      }
      return '<a href="'.$str.'" target="_blank" title="'.$str.'">'.$cut_link.'</a>';
}

$str = '[url]http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21124048.html[/url]';
$search = array('#\[url]([a-z0-9]+?://){1}([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)\[/url\]#');
$str = preg_replace_callback($search, 'shorten_url', $str);
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now