Link to home
Start Free TrialLog in
Avatar of chrisnetonline
chrisnetonlineFlag for United States of America

asked on

Concatenated URL's - RegEx


$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: https://www.experts-exchange.com/Web/Web_Languages/PHP/askQuestion.jsp
UPDATED: http://www.experts-exch...HP/askQuestion.jsp
Avatar of Zyloch
Zyloch
Flag of United States of America image

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
Oops, this: substr($1.$2,$f_len,($h-$f_len)); should be
substr($1.$2,$f_len,($l-$f_len));

Regards
Avatar of chrisnetonline

ASKER

how would this be implemented though? i dont see how that code is being run on the URL...
Try putting it on the line after $search = blah but before $replace = blah
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]https://www.experts-exchange.com/questions/21124048/Concatenated-URL's-RegEx.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
Try changing this line:

$f_len=strlen($checkEnd(count($checkEnd)-1))+3; to
$f_len=strlen($checkEnd[count($checkEnd)-1])+3;
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
ASKER CERTIFIED SOLUTION
Avatar of Zyloch
Zyloch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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]https://www.experts-exchange.com/questions/21124048/Concatenated-URL's-RegEx.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);