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 7248663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:10:09+00:00 2026-05-28T22:10:09+00:00

I need a systematic way of replacing each word in a string separately by

  • 0

I need a systematic way of replacing each word in a string separately by providing my own input for each word. I want to do this on the command line.

So the program reads in a string, and asks me what I want to replace the first word with, and then the second word, and then the third word, and so on, until all words have been processed.

The sentences in the string have to remain well-formed, so the algorithm should take care not to mess up punctuation and spacing.

Is there a proper way to do this?

  • 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-28T22:10:09+00:00Added an answer on May 28, 2026 at 10:10 pm

    Given some text

    $subject = <<<TEXT
    I need a systematic way of replacing each word in a string separately by providing my own input for each word. I want to do this on the command line.
    
    So the program reads in a string, and asks me what I want to replace the first word with, and then the second word, and then the third word, and so on, until all words have been processed.
    
    The sentences in the string have to remain well-formed, so the algorithm should take care not to mess up punctuation and spacing.
    
    Is there a proper way to do this?
    TEXT;
    

    You first tokenize the string into words and “everything else” tokens (e.g. call them fill).
    Regular expressions are helpful for that:

    $pattern = '/(?P<fill>\W+)?(?P<word>\w+)?/';
    $r = preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
    

    The job is now to convert the return value into a more useful data-structure, like an array of tokens and an index of all words used:

    $tokens = array(); # token stream
    $tokenIndex = 0;
    $words = array(); # index of words
    foreach($matches as $matched)
    {
        foreach($matched as $type => $match)
        {
            if (is_numeric($type)) continue;
            list($string, $offset) = $match;
            if ($offset < 0) continue;
    
    
            $token = new stdClass;
            $token->type = $type;
            $token->offset = $offset;
            $token->length = strlen($string);
    
            if ($token->type === 'word')
            {
                if (!isset($words[$string]))
                {
                    $words[$string] = array('string' => $string, 'tokens' => array());
                }
                $words[$string]['tokens'][] = &$token;
                $token->string = &$words[$string]['string'];
            } else {
                $token->string = $string;
            }
    
    
            $tokens[$tokenIndex] = &$token;
            $tokenIndex++;
            unset($token);
        }
    }
    

    Exemplary you can then output all words:

    # list all words
    
    foreach($words as $word)
    {
        printf("Word '%s' used %d time(s)\n", $word['string'], count($word['tokens']));
    }
    

    Which would give you with the sample text:

    Word 'I' used 3 time(s)
    Word 'need' used 1 time(s)
    Word 'a' used 4 time(s)
    Word 'systematic' used 1 time(s)
    Word 'way' used 2 time(s)
    Word 'of' used 1 time(s)
    Word 'replacing' used 1 time(s)
    Word 'each' used 2 time(s)
    Word 'word' used 5 time(s)
    Word 'in' used 3 time(s)
    Word 'string' used 3 time(s)
    Word 'separately' used 1 time(s)
    Word 'by' used 1 time(s)
    Word 'providing' used 1 time(s)
    Word 'my' used 1 time(s)
    Word 'own' used 1 time(s)
    Word 'input' used 1 time(s)
    Word 'for' used 1 time(s)
    Word 'want' used 2 time(s)
    Word 'to' used 5 time(s)
    Word 'do' used 2 time(s)
    Word 'this' used 2 time(s)
    Word 'on' used 2 time(s)
    Word 'the' used 7 time(s)
    Word 'command' used 1 time(s)
    Word 'line' used 1 time(s)
    Word 'So' used 1 time(s)
    Word 'program' used 1 time(s)
    Word 'reads' used 1 time(s)
    Word 'and' used 5 time(s)
    ... (and so on)
    

    Then you do the job on the word tokens only. For example replacing one string with another:

    # change one word (and to AND)
    
    $words['and']['string'] = 'AND';
    

    Finally you concatenate the tokens into a single string:

    # output the whole text
    
    foreach($tokens as $token) echo $token->string;
    

    Which gives with the sample text again:

    I need a systematic way of replacing each word in a string separately by providing my own input for each word. I want to
     do this on the command line.
    
    So the program reads in a string, AND asks me what I want to replace the first word with, AND then the second word, AND 
    then the third word, AND so on, until all words have been processed.
    
    The sentences in the string have to remain well-formed, so the algorithm should take care not to mess up punctuation AND
     spacing.
    
    Is there a proper way to do this?
    

    Job done. Ensure that word tokens are only replaced with valid word tokens, so tokenize the user-input as well and give errors if it’s not a single word token (does not matches the word pattern).

    Code/Demo

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

Sidebar

Related Questions

Need to parse a Class declaration line in Java using regular expression e.g. String
Need the solution to solve taking string from the database and replacing the parameter.
Need a way to allow sorting except for last item with in a list.
Need some help about with Memcache. I have created a class and want to
need a little help with this one. I have a form that I am
Need some insight on how this works I have the following piece of code
I have an input string I'm trying to parse. It might look like either
Need a way to navigate/browse XSLT files easily with Vim. Similar to the way
Need a div to partially show the image behind it, is this even possible?
Need to calculate optimum ulimit and fs.file-max values according to my own server needs.

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.