[ create a new paste ] login | about

Link: http://codepad.org/7FME5kMe    [ raw code | output | fork ]

PHP, pasted on Aug 9:
<?php
$string = 'Hey they ey sashey play 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 $lastTwo
        if ( substr($word, -2) == $lookFor )
        {
            $string[$key] =  substr_replace($word, $switchTo, -2);
        }
    }
}
  // Recreate string from array
$string = implode(" ", $string);
  // See what we got
echo $string;
?>


Output:
1
Hey thei ey sashei play nowhei


Create a new paste based on this one


Comments: