Solved

Concatenated URL's - RegEx

Posted on 2004-09-08
9
232 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
how to use Initialization Vector for openssl_encrypt() 5 58
Decrypt string by php 7 47
What's wrong with this PDO query? 5 27
Moving from Mcrypt to OpenSSL 18 45
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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 …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

856 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