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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:30:13+00:00 2026-05-16T04:30:13+00:00

We have a variable $string , its contains some text like: About 200 million

  • 0

We have a variable $string, its contains some text like:

About 200 million CAPTCHAs are solved by humans around the world every day.

How can we get 2-3 last or first letters of each word (which length is more than 3 letters)?

Will check them for matched text with foreach():

if ('ey' is matched in the end of some word) {
  replace 'ey' with 'ei' in this word;
}

Thanks.

  • 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-16T04:30:14+00:00Added an answer on May 16, 2026 at 4:30 am

    First, I’ll give you an example of how to loop through a string and work with each word in the string.

    Second, I’ll explain each part of the code so that you can modify it to your exact needs.


    Here is how to switch out the last 2 letters (if they are “ey”) of each word that is more than 3 letters long.

    <?php
      // Example string
    $string = 'Hey they ey shay play stay nowhey';
    
      // Create array of words splitting at spaces
    $string = explode(" ", $string);
    
      // The search and replace strings
    $lookFor = "ey";
    $switchTo = "ei";
    
      // Cycle through the words
    foreach($string as $key => $word)
    {
          // If the word has more than 3 letters
        if(strlen($word) > 3)
        {
              // If the last two letters are what we want
            if ( substr($word, -2) == $lookFor )
            {
                  // Replace the last 2 letters of the word
                $string[$key] =  substr_replace($word, $switchTo, -2);
            }
        }
    }
      // Recreate string from array
    $string = implode(" ", $string);
      // See what we got
    echo $string;
      // The above will print:
      // Hey thei ey sashei play nowhei
    ?>
    

    Live example


    I’ll explain each function so that you can modify the above to exactly how you want it, since I don’t precisely understand all your specifications:

    1. explode() will take a string and split it apart into an array. The first argument is what you use to split it. The second argument is the string, so explode(" ", $string) will split $string by the use of spaces. The spaces will not be included in the array.
    2. foreach() will cycle through each element of an array. foreach($string as $key => $word) will go through each element of $string and for each element it will assign the index number to $key and the value of the element (the word in this case) to $word.
    3. strlen() returns how long a string is.
    4. substr() returns a portion of a string. The first argument is the string, the second argument is where the substring starts, and a third optional argument is the length of the substring. With a negative start, the start will be calculated from the end of the string to the end of the string. In other words, substr($word, -2) returns the substring that begins two from the end of the string and goes to the end of the string…. the last two letters. If you want the first two letters, you would use substr($word, 0, 2), since you’re starting at the very beginning and want a length of 2 letters.
    5. substr_replace() will replace a substring within a string. The first argument is the entire string. The second argument is your replacement substring. The third argument is where the replacement starts, and the fourth optional argument is the length of the substring, so substr_replace($word, $switchTo, -2) will take $word and starting at the penultimate letter, replace what’s there with $switchTo. In this case, we’ll switch out the last two letter. If you want to replace the first two letters, you would use substr_replace($word, $switchTo, 0, 2)
    6. implode() is the opposite of explode. It takes an array and forms it into a string using the separator specified.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: SELECT p.id as a, p.url as b, t.id as… May 17, 2026 at 3:22 am
  • Editorial Team
    Editorial Team added an answer There are two parts to the problem First Issue You… May 17, 2026 at 3:19 am
  • Editorial Team
    Editorial Team added an answer I thought I'd show the regex approach, too. It doesn't… May 17, 2026 at 3:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

When using CDT I would like to have std::string show up in the 'variable'
I have a string variable that represents the name of a custom class. Example:
If I have variable of type IEnumerable<List<string>> is there a LINQ statement or lambda
In Java, suppose I have a String variable S, and I want to search
I have a variable of type Hashmap <String,Integer >. In this, the Integer value
I have the following string in the smarty (php templating system) variable $test: <img
I have a java string, which has a variable length. I need to put
I have six string variables say str11, str12, str13, str21, str21 and str23. I
If I have two variables: Object obj; String methodName = getName; Without knowing the
I have a variable of type Dynamic and I know for sure one of

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.