Skip to content

Instantly share code, notes, and snippets.

@TheDeadCode
Created January 11, 2018 00:13
Show Gist options
  • Save TheDeadCode/6d254eb6e556443768307b96df403b3c to your computer and use it in GitHub Desktop.
Save TheDeadCode/6d254eb6e556443768307b96df403b3c to your computer and use it in GitHub Desktop.

Revisions

  1. TheDeadCode created this gist Jan 11, 2018.
    20 changes: 20 additions & 0 deletions Examples.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    Feed it this comment:
    ```
    Here is where you can register API routes for your application. These routes are loaded by the RouteServiceProvider within a group which is assigned the "api" middleware group. Enjoy building your API!
    ```

    It will return this:
    ```
    Here is where you can register API routes for your application. These
    routes are loaded by the RouteServiceProvider within a group which
    is assigned the "api" middleware group. Enjoy building your API!
    ```

    You can then format it using whatever IDE you like:
    ```php
    /**
    * Here is where you can register API routes for your application. These
    * routes are loaded by the RouteServiceProvider within a group which
    * is assigned the "api" middleware group. Enjoy building your API!
    */
    ```
    74 changes: 74 additions & 0 deletions comments.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    <?php

    $query = $argv[1];

    $lastchar = $query[strlen($query) - 1];
    if ( ! in_array($lastchar, ['.', '!', '?', ';'])) {
    $query .= '.';
    }

    if (($strlen = strlen($query)) > 75) {
    $words = explode(' ', $query);
    $sentence = [];

    // We now have an array of words, by char
    foreach ($words as $word) {
    $sentence[] = str_split($word);
    }

    exit(getRows($sentence));
    } else {
    exit($query);
    }

    function getRows(array $sentence, $max_per_row = 75, $best_try = null)
    {
    if ($max_per_row < 50) {
    return $best_try;
    }
    $current_limit = $max_per_row;
    $temp = '';
    $current_line_count = 0;
    $current_line = 1;
    $force_recurse = false;
    // We will iterate over each until we are over the limit of 75 chars
    for ($i = 0; $i <= count($sentence) - 1; $i++) {
    $word = $sentence[$i];
    // If the word is way too big
    if (count($word) >= $current_limit) {
    // Then the current word should be on its own line
    if ($current_line_count > 0) {
    $temp .= "\n";
    $current_line++;
    $current_line_count = 0;
    }
    $temp .= implode('', $word)."\n";
    $current_line++;

    continue;
    }

    if ($current_line_count + count($word) > $current_limit) {
    // Check how long there is blank
    if (($current_line_count / $current_limit) <= 0.75) { // At least 50% is blank
    $force_recurse = true;
    break;
    }
    // The next word is too long for the line
    $current_limit = $current_line_count - 2; // 1 to remove the whitespace, and another one to make it smaller
    $current_line_count = 0;
    $current_line++;
    $temp = trim($temp);
    $temp .= "\n";
    }

    $temp .= implode('', $word).' ';
    $current_line_count += count($word) + 1; // To add the whitespace
    }

    if ($force_recurse || (($current_line_count - 1) / $current_limit) <= 0.75) {
    return getRows($sentence, $max_per_row - 2, $temp);
    } else {
    return trim($temp);
    }
    }