tags:

views:

150

answers:

3
+4  Q: 

PHP string letters

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.

+2  A: 
$string = 'About 200 million CAPTCHAs are solved by humans around the world every day.';
$result = array();
$words = explode(" ",$string);
foreach($words as $word){
 if(strlen($word) > 3){
  $result[] = substr($word,0,3); //first 3 characters, use "-3" for second paramter if you want last three
 }
}
Mike Sherov
It should be more specific, question is updated.
Happy
Inside of the if(), you could add this if you wanted both first and last three: `$result[] = substr($word, strlen($word)-3);`
WillyG
Of course the downvote comes after the OP changes the question...
Mike Sherov
+1  A: 
function get_symbols($str, $reverse = false)
{
    $symbols = array();   

    foreach (explode(' ', $str) as $word)
    {
        if ($reverse) 
          $word = strrev($word);

        if (strlen($word) > 3)
          $word = substr($word, 0, 3);

        array_push($symbols, $word);
    }

    return $symbols;
}

EDIT:

function change_reverse_symbol_in_word($str, $symbol, $replace_to)
{
    $result = ""; 

    foreach (explode(' ', $str) as $word)
    {
        $rword = $word;

        if (strlen($rword) > 3)
        {
            $rword = substr($word, 0, -3);
        }

        if (!strcmp($symbol, $rword))
        {
            $word = substr($word, 0, strlen($word) - strlen($rword)) . $replace_to;
        }

        $result .= $word . " ";
    }

    return $result;
}

And if you want to use this like a your question you must call this like that:

$string_malformed = change_reverse_symbol_in_word($str, "ey", "ei");
Svisstack
what about last or first symbols?
Happy
@Ignatz: You have in function second argument bool determinating what symbols you want to get, first or last; if you want get last then set bool to true value.
Svisstack
@Svisstack: I've updated the question.
Happy
You should ask next question
Svisstack
@Svisstack: it will be all in one.
Happy
+2  A: 

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.
Peter Ajtai
thanks man, looks perfect. Can we check for ar array, instead of strings? $lookFor = array(ey, ay); $switchTo = array(ei, ai);
Happy
@Ignatz - Seems like you'd want to use something like: `if ( in_array( substr($word, -2), $lookFor ) )` to match the end of the word to the $lookFor array. Then you could just replace the very last letter with `i`, since the other letter is staying unchanged. ---- ( http://php.net/manual/en/function.in-array.php )
Peter Ajtai