String shortening for long URLs in Perl and PHP

## Perl #

my $page_link;
if ($val[5]) {
# construct link if value is not NULL (undef) or empty
my $linktext = $val[5];
$linktext = substr($linktext, 0, 27) . "..." . substr($linktext, -
+10)
if length($val[5]) > 40;

$page_link = a ({-href => "$val[5]"}, $linktext);

## PHP original  ##

$truncated = (strlen($string) > 20) ? substr($string, 0, 20) . '...' : $string;

## PHP  ##

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{

$linktext = (strlen($row['url']) > 85) ? substr($row['url'], 0, 85) . '...' : $row['url'];

if ($row['publish']=="1")
{
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
<td align="left"><a href="' . $row['url'] . '">' . $linktext . '</a></td>
<td align="left">' . $row['date_mdy'] . '</td>
<td align="left">' . $row['status'] . '</td>
<td align="left">' . $row['updated'] . '</td>

</tr>
';
}
else
{
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
<td align="left"><a href="' . $row['url'] . '">' . $linktext . '</a></td>
<td align="left">' . $row['date_mdy'] . '</td>
<td align="left">' . $row['status'] . '</td>
<td align="left">' . $row['updated'] . '</td>

</tr>
';
}

Leave a Reply

Your email address will not be published. Required fields are marked *