chunk_split

  • Splitting a long string into smaller chunks can be done in Ruby using a sequence of:

    1. String#scan the string into the specified chunk size
    2. Array#join the string back together using the ending sequence as the glue
    3. String#+ appends the final ending sequence to the result

    PHP

    $result = chunk_split("a really long string", 5, "\r\n");
    var_export($result);
    // => "a rea\r\nlly l\r\nong s\r\ntring\r\n"

    Ruby

    p "a really long string".scan(/.{1,5}/).join("\r\n") + "\r\n"
    # => "a rea\r\nlly l\r\nong s\r\ntring\r\n"

    see also

chr class_exists

Looking for Rails or PHP web application development, integration, and training?

Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.