Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Concatenated URL's - RegEx

Posted on 2004-09-08
9
Medium Priority
?
240 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
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.
Suggested Courses

916 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