over 7 years ago

While working on various web projects written completely in PHP, I collected
some custom procedures for more satisfying work.

Text trimming

Often you are in need of trimming some text by set number of characters, but
do not want to trim mid-word. This is a function which cuts text between words
trying to reduce string length to be less than provided value.

// Cut text at set length, 
// preserving words and appending 
// ellipsis at the end of trimmed string
function my_ellipsis_trim($text, $length) {
    $count = strlen($text);
    $tlen = $length - 3;
    // If text is shorter than needed length, 
  //  return it untouched
 if ($tlen > $count) {
        return $text;
    } else {
  // Searching index of last space in the string
  // So text will be cut at last whole word
     for ($cutidx = $tlen; $cutidx >= 0; $cutidx--) {
            if ($text{$cutidx} == " ") break;
        return substr($text, 0, $cutidx) . " ...";

Pretty-printing values of variables

So, you are a web developer. Why are you have to use such stdout-oriented
functions as echo or print? We will dump variables of any complexity using
HTML format.

Additionally, as we know about the existence of highlight.js, we want
to colorize the output if it is sufficiently complex.

// $data is a value to dump
// $caption will be printed as a header before the $data
// $method controls selection of internal printing function
//   choice is between 'print_r' and 'var_dump'
// $is_code marks if the data should be wrapped in <code> tag 
//   for processing it with highlight.js
// $escape controls if the data should be processed 
//   by htmlspecialchars
// returns string containing formatted $data dump
function my_report(
  $caption = 'Data', 
  $method = 'print_r', 
  $is_code = false, 
  $escape = true) {
    $result = '<pre><strong>'.$caption.' : </strong></pre><pre>';
    $result .= $is_code ? '<code>' : '';
    switch ($method){
        case 'print_r':
            print_r($data); break;
        case 'var_dump':
            var_dump($data); break;
    $a = ob_get_contents();
// Escape every HTML special chars (especially &rt; and < )
 $b = $escape ? htmlspecialchars($a, ENT_QUOTES) : $a;
    $result .= $b;
    $result .= $is_code ? '</code>' : '';
    $result .= '</pre>';
    return $result;

MIME encoding of strings

When sending e-mail letters in real multi-national world you need to declare
character encoding on almost anything. To do this in e-mail headers, you should
wrap your header content in special encoding declaration and convert it to
Base64. To speed-up this process, I wrote small helper function.

function my_mime_header($string, $encoding=null) {
    $encoding = mb_internal_encoding();
    return "=?$encoding?B?" . base64_encode($string) . "?=";

Inspired by comment to mb_encode_mimeheader() at php.net.

comments powered by Disqus