Jump to content

Limiting charactors returned.


Snooble

Recommended Posts

Hello people,

 

I'm echoing out table cell kept in an array using:

$list['email']

 

It echos the user's email address. But if i had an email address that was: [email protected]

 

it would strech the table. I want to limit the amount of charactors. put ...'s inbetween

 

Limit the output to 10 charactors.

 

Any help?

 

Thanks

 

Snooble

Link to comment
https://forums.phpfreaks.com/topic/40903-limiting-charactors-returned/
Share on other sites

In case the matter is urgent here is a quick one:

 


<?PHP

$email = "get this from somewhere"
$max_length = "20";

if(strlen($email) > $max_length){

  $email = substr($email, 0, $max_length) . "...";

}

?>

 

This will reduce the e-mail to a length of $max_length and add 3 dots at the end, but this wont be very pretty, since it would return "sometext...." without @ or anything if the localpart of the email ([email protected]) is longer that $max_length... a more sophisticated one will come in a few minutes :)

Hmm... a few minutes - make that days ;)

 

I promised to write a more sophisticated function to shorten e-mail addresses to a specific length - so here we go:

 

<?PHP

/*********************************************************************************
* shorten_email
*   shorten_email - shortens an e-mail address
*
*
* Description
*   string shortenemail(string $email, int $length [, string $replacement])
*       
*   Shortens _email_ to a length defined by _length_ and replaces the
*   excess with _replacement_
*
*
* Parameters
*   email: e-mail address to shorten
*   length: the desired length of the email
*   replacement: if _replacement_ is supplied it will be inserted into the
*                middle of both the local part and the domain part of the
*                e-mail to represent the missing characters
*                
*
*
* Return values
*   shorten_email() returns the shortened e-mail address or false if an error
*   occurred.
*
* Note
*   shorten_email() does not validate the e-mail address and if a non valid
*   e-mail address is supplied it will be processed but return an obscure
*   result. The e-mail adress must be formatted as [email protected]
*   shorten_email() will return false if the supplied length argument is not
*   numeric, below zero or equal to zero.
*   shorten_email() will return false if the length of the supplied replacement
*   argument exceeds the the length of the local part or domain part.
*********************************************************************************/

function shorten_email($email,$length,$replacement = ""){

  if( is_numeric($length) && ($length > 0) ){

    if(strlen($email) > $length){
       
      //Splitting the email at @ to get the local part seperated..
      $split1 = explode("@",$email);
      $local = $split1[0];  
  
      //Splitting the rest at . (dot) to get the domain and tld seperated..     
      $split2 = explode(".",$split1[1]);
      $domain = $split2[0];
      $tld = $split2[1];
      
      //Calculating the max length for local and domain
      $local_max_length = floor($length / 2)-1;
      $domain_max_length = $length - ($local_max_length + strlen($tld) + 2);
      
      //Calculating the new length for local and domain
      if( (strlen($local) > $local_max_length) && (strlen($domain) < $domain_max_length) ){
        
        $new_local_length = $local_max_length + ($domain_max_length - strlen($domain));
        $new_domain_length = strlen($domain);
      }
      elseif( (strlen($local) < $local_max_length) && (strlen($domain) > $domain_max_length) ){
        
        $new_local_length = strlen($local);
        $new_domain_length = $domain_max_length + ($local_max_length - strlen($local));
      }
      else{
        
        $new_local_length = $local_max_length;
        $new_domain_length = $domain_max_length;
      }
  
      //Checking if the replacement argument is usable
      $replacement_length = strlen($replacement);
      if( ($replacement_length <= ($new_local_length - 2)) || ($replacement_length <= ($new_domain_length - 2)) ){
        
   
        //Shortening the local part
        if(strlen($local) > $new_local_length){
          
          $newlocal = substr($local,0,floor(($new_local_length - $replacement_length) / 2));
          $newlocal .= $replacement;
          $newlocal .=substr($local,(strlen($local) - ceil(($new_local_length - $replacement_length) / 2 )),strlen($local));
          
        }
        else{
          $newlocal = $local;
        }
        
      
        //Shortening the domain
        if(strlen($domain) > $new_domain_length){
        
          $newdomain = substr($domain,0,floor(($new_domain_length - $replacement_length)/2));
          $newdomain .= $replacement;
          $newdomain .= substr($domain,(strlen($domain) - ceil(($new_domain_length - $replacement_length) / 2)),strlen($domain));
        
        }
        else{
          $newdomain = $domain;
        }
        
        //Reassembling the email
        $shortemail = $newlocal . "@" . $newdomain . "." . $tld;
        
        //Returning the short email
        return $shortemail;
        
      }
      else{
      
        return false;
        
      }
        
    }
    else{
        
      //Returning the email
      return $email;
       
    }
  
  }
  else{
    return false;
  }
  
}
?>

 

The function and how to use it is described in the code comments but here is the essence of it:

 

shorten_email($email, $length, $replacement)

$email: the email you want to shorten
$length: the desired length of the email
$replacement (optional): a string which you want to replace the excess with...

 

you can try it out here: http://wuhtzu.dk/random/shortenemail.php (my server seems a little slow today so bear with it)

 

I have tested it with a timer a few times and shortening 1000 emails (the same email every time) took maximum 1.2sec but most of the time just 0.03sec (as i said my host seem a little slow today)...

 

Wuhtzu

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.