pgsql Return Partial Text Field Ending In A Whole Word
Something like this should work (here 15 is the equivalent of $maxlength):

SELECT
  CASE WHEN LENGTH(mycolumn) &lt;= 15
       THEN mycolumn
  ELSE SUBSTRING(mycolumn, 0, 15 + 1 -
                 POSITION(' ' IN REVERSE(SUBSTRING(mycolumn, 0, 15 + 1))))
  END
  FROM mytable


Here is a working demo.