开发者

PHP string letters

We h开发者_如何转开发ave 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.


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.


$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
 }
}


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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜