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.

Comments

what you mean when you saying 'symbols of each word'?

Written by Svisstack

"million": "m, i, l" are the first three symbols, "i, o, n" - last 3 symbols

Written by Messi

Your question would be immediately easier to understand if you used "letters" instead of "symbols".

Written by meagar

@meagar thanks for the point, done.

Written by Messi

Accepted Answer

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.
Written by Peter Ajtai
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki