Pin Cody
Pin Cody

Reputation: 135

get the last character in a string

I have a function to replace each character by array:

function z3($str){
  $text=$str;
  $sr = array(
    'a' => array('A'),
    'b' => array('B'),
    ' ' => array(' ')
  ); 
  for ($i = 0, $len = strlen($text); $i < $len; $i++) { 
    $news .= $sr[$text[$i]][@array_rand($sr[$text[$i]])];   
  } 
  $nn=$news; 
  return $nn;
}
echo z3("aaa");

And I need to develop it to if the character (a) was in the last word and after it is a space, I want to add ('s) to it. Let me explain:

$str = "aaa aaa aaa";
echo z3($str);

I'd need the output to be AAA's AAA's AAA.

Upvotes: 0

Views: 344

Answers (5)

ghoti
ghoti

Reputation: 46886

function z3($str) {
  $patt=str_split("ab");
  $repl=str_split("AB");
  return preg_replace("/ /", "'s ", str_replace($patt, $repl, $str));
}

$str="bar tab";
echo z3($str);

>>> BAr's tAB

Upvotes: 0

benastan
benastan

Reputation: 2278

You want to (1) change each character to its corresponding value in $sr array and (2) if the word contains an 'a' and is not the last word, add "'s" to the end of the word.

Try:

    function z3(&$str) {
        $sr = array(
            'a' => array('A'),
            'b' => array('B'),
            ' ' => array(' ')
        );
        $words = explode(' ', $str); // split str into words
        $numwords = count($words);
        foreach ($words as $index => &$word) { // run through each word
            $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); // split word into characters
            foreach ($chars as &$char) { // run through each character
                if (array_key_exists($char, $sr)) {
                    $char = $sr[$char]; // if there is a replacement in $sr array, set $char to its value
                }
            }
            if ($index < $numwords && in_array($sr['a'], $chars) { // if this is not the last word and it contains an 'a', append characters ' and s
                $chars += array("'", "s");
            }
            $word = implode('', $chars); // pull the words back together
        }
        $str = implode(' ', $words); // pull the string back together
    }

    $str = 'aaa aaa aaa';
    z3($str);
    echo $str; // echoes AAA's AAA's AAA

Upvotes: 0

user895378
user895378

Reputation:

It's difficult to understand your question, but if you want to only add apostrophes to words whose last character is a and you needed to capitalize everything but the 's and you didn't want to make the final word possessive ...

function z3($str) {

  $parts = explode(' ', $str);

  for ($i=0; $i < count($parts); $i++) {
    $parts[$i] = strtoupper($parts[$i]);
    if (substr($parts[$i], -1) == 'A') {
      $parts[$i] = $parts[$i] . "'s";
    }
  }

  $str = implode(' ', $parts);
  return rtrim($str, "'s");
}

$str = "aaa aaa aaa";
echo z3($str); // outputs AAA's AAA's AAA

Upvotes: 1

Jazi
Jazi

Reputation: 6752

function z3($str) {
    $sr = array(
        'a' => array('A'),
        'b' => array('B'),
        ' ' => array(' ')
    );

    $nn = '';

    for ($i = 0, $len = strlen($str); $i < $len; $i++) {
        if($str[$i] == ' ' && isset($str[$i-1]) && $str[$i-1] == 'a')
            $nn.= "'s";
        $nn.= $sr[$str[$i]][@array_rand($sr[$str[$i]])];   
    }

    return $nn;
}

$str = "aaa aaa aaa";
echo z3($str);

Upvotes: 0

Steve Robbins
Steve Robbins

Reputation: 13822

str_replace(" ", "'s ", trim($str))

?

Upvotes: 0

Related Questions