If string too long then trim it

  $MyString = strlen($MyString) > 500 ? substr($MyString,0,500) : $MyString;

  $MyString = strlen($MyString) > 500 ? substr($MyString,0,497) . "..." : $MyString;

Remove last # characters

  if (strlen($MyString >= 3))
    $MyString_Modified = substr($MyString, 0, (strlen($MyString) - 3) );
  else
    $MyString_Modified = "";

Remove middle characters of a filename

//***********************************************
//***********************************************
//********** CROP FILENAME IF TOO LONG **********
//***********************************************
//***********************************************
//Middle of filename string is cropped, with "___" inserted
function CropFilenameStringIfTooLong ($FileName, $MaxLength)
{
  if (strlen($FileName) > $MaxLength)
  {
    //TOO LONG
    $keep = $MaxLength - 3;   //Space left after inserting "___"
    $left = ceil($keep / 2);
    $right = floor($keep / 2);
    $FileName = substr($FileName, 0, $left) . "___" . substr($FileName, -$right);
  }

  return($FileName);
}

Limit URL length

//******************************************
//******************************************
//********** CROP URL IF TOO LONG **********
//******************************************
//******************************************
//First any "https://" is removed if too long, then any arguments if still too long
//If still too long the middle of URL string is cropped, with "___" inserted
function CropUrlStringIfTooLong ($Url, $MaxLength)
{
  if (strlen($Url) > $MaxLength)
  {
    //TOO LONG - remove "https://"

    if (str_starts_with($Url, 'http://'))
      $Url = substr($Url, 7);
    else if (str_starts_with($Url, 'https://'))
      $Url = substr($Url, 8);

    if (strlen($Url) > $MaxLength)
    {
      //STILL TOO LONG - remove arguments
      $qpos = strpos($Url, '?');
      if ($qpos !== False)
        $Url = substr($Url, 0, $qpos);
      
      if (strlen($Url) > $MaxLength)
      {
        //STILL TOO LONG - crop in center
        $keep = $MaxLength - 3;   //Space left after inserting "___"
        $left = ceil($keep / 2);
        $right = floor($keep / 2);
        $Url = substr($Url, 0, $left) . "___" . substr($Url, -$right);
      }
    }
  }

  return($Url);
}