{"id":1561,"date":"2014-11-28T16:42:43","date_gmt":"2014-11-28T16:42:43","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=1561"},"modified":"2025-10-03T10:22:18","modified_gmt":"2025-10-03T09:22:18","slug":"limit-string-length","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/strings\/limit-string-length","title":{"rendered":"Limit string length"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">If string too long then trim it<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyString = strlen($MyString) &gt; 500 ? substr($MyString,0,500) : $MyString;\n\n  $MyString = strlen($MyString) &gt; 500 ? substr($MyString,0,497) . \"...\" : $MyString;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Remove last # characters<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  if (strlen($MyString &gt;= 3))\n    $MyString_Modified = substr($MyString, 0, (strlen($MyString) - 3) );\n  else\n    $MyString_Modified = \"\";<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Remove middle characters of a filename<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/***********************************************\n\/\/***********************************************\n\/\/********** CROP FILENAME IF TOO LONG **********\n\/\/***********************************************\n\/\/***********************************************\n\/\/Middle of filename string is cropped, with \"___\" inserted\nfunction CropFilenameStringIfTooLong ($FileName, $MaxLength)\n{\n  if (strlen($FileName) &gt; $MaxLength)\n  {\n    \/\/TOO LONG\n    $keep = $MaxLength - 3;   \/\/Space left after inserting \"___\"\n    $left = ceil($keep \/ 2);\n    $right = floor($keep \/ 2);\n    $FileName = substr($FileName, 0, $left) . \"___\" . substr($FileName, -$right);\n  }\n\n  return($FileName);\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Limit URL length<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/******************************************\n\/\/******************************************\n\/\/********** CROP URL IF TOO LONG **********\n\/\/******************************************\n\/\/******************************************\n\/\/First any \"https:\/\/\" is removed if too long, then any arguments if still too long\n\/\/If still too long the middle of URL string is cropped, with \"___\" inserted\nfunction CropUrlStringIfTooLong ($Url, $MaxLength)\n{\n  if (strlen($Url) > $MaxLength)\n  {\n    \/\/TOO LONG - remove \"https:\/\/\"\n\n    if (str_starts_with($Url, 'http:\/\/'))\n      $Url = substr($Url, 7);\n    else if (str_starts_with($Url, 'https:\/\/'))\n      $Url = substr($Url, 8);\n\n    if (strlen($Url) > $MaxLength)\n    {\n      \/\/STILL TOO LONG - remove arguments\n      $qpos = strpos($Url, '?');\n      if ($qpos !== False)\n        $Url = substr($Url, 0, $qpos);\n      \n      if (strlen($Url) > $MaxLength)\n      {\n        \/\/STILL TOO LONG - crop in center\n        $keep = $MaxLength - 3;   \/\/Space left after inserting \"___\"\n        $left = ceil($keep \/ 2);\n        $right = floor($keep \/ 2);\n        $Url = substr($Url, 0, $left) . \"___\" . substr($Url, -$right);\n      }\n    }\n  }\n\n  return($Url);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If string too long then trim it Remove last # characters Remove middle characters of a filename Limit URL length<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-1561","post","type-post","status-publish","format-standard","hentry","category-strings"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1561","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=1561"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1561\/revisions"}],"predecessor-version":[{"id":5154,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1561\/revisions\/5154"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=1561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=1561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=1561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}