Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7439961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:46:51+00:00 2026-05-29T10:46:51+00:00

I’m going to make a word wrap algorithm in PHP. I want to split

  • 0

I’m going to make a word wrap algorithm in PHP. I want to split small chunks of text (short phrases) in n lines of maximum m characters (n is not given, so there will be as much lines as needed). The peculiarity is that lines length (in characters) has to be much balanced as possible across lines.

Example of input text:

How to do things

Wrong output (this is the normal word-wrap behavior), m=6:

How to
do
things

Desired output, always m=6:

How 
to do 
things

Does anyone have suggestions or guidelines on how to implement this function? Basically, I’m searching something for pretty print short phrases on two or three (as much as possible) equal length lines.


Update: It seems I’m searching exactly for a Minimum raggedness word wrap algorithm. But I can’t find any implementation in a real programming language (anyone, then I can convert it in PHP).


Update 2: I started a bounty for this. Is it possible that do not exist any public implementation of Minimum raggedness algorithm in any procedural language? I need something written in a way that can be translated into procedural instructions. All I can find now is just a bounch of (generic) equation that however need a optimal searching procedure. I will be grateful also for an implementation that can only approximate that optimal searching algorithm.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:46:52+00:00Added an answer on May 29, 2026 at 10:46 am

    Quick and dirty, in c++

    #include <sstream>
    #include <iostream>
    #include <vector>
    #include <cstdlib>
    #include <memory.h>
    
    using namespace std;
    
    int cac[1000][1000];
    string res[1000][1000];
    vector<string> words;
    int M;
    
    int go(int a, int b){
        if(cac[a][b]>= 0) return cac[a][b];
        if(a == b) return 0;
    
        int csum = -1;
        for(int i=a; i<b; ++i){
        csum += words[i].size() + 1;
        }
        if(csum <= M || a == b-1){
        string sep = "";
            for(int i=a; i<b; ++i){
                res[a][b].append(sep);
                res[a][b].append(words[i]);
                sep = " ";
        }
        return cac[a][b] = (M-csum)*(M-csum);
        }
    
        int ret = 1000000000;
        int best_sp = -1;
        for(int sp=a+1; sp<b; ++sp){
        int cur = go(a, sp) + go(sp,b);
        if(cur <= ret){
            ret = cur;
            best_sp = sp;
        }
        }
        res[a][b] = res[a][best_sp] + "\n" + res[best_sp][b];
        return cac[a][b] = ret;
    }
    
    
    int main(int argc, char ** argv){
        memset(cac, -1, sizeof(cac));
        M = atoi(argv[1]);
        string word;
        while(cin >> word) words.push_back(word);
        go(0, words.size());
        cout << res[0][words.size()] << endl;
    }
    

    Test:

    $ echo "The quick brown fox jumps over a lazy dog" |./a.out 10
    The quick
    brown fox
    jumps over
    a lazy dog
    

    EDIT: just looked at the wikipedia page for minimum raggedness word wrap. Changed algorithm to the given one (with squared penalties)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.