Solved

Concatenated URL's - RegEx

Posted on 2004-09-08
9
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 12012999
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
ID: 12013039
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
ID: 12070588
how would this be implemented though? i dont see how that code is being run on the URL...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 36

Expert Comment

by:Zyloch
ID: 12071058
Try putting it on the line after $search = blah but before $replace = blah
0
 

Author Comment

by:chrisnetonline
ID: 12071896
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
ID: 12078939
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
ID: 12079348
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
ID: 12080399
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
ID: 12081222
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

733 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