0

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.

5
  • u want uppercase of each word and any space with 's. am i right? Commented Dec 12, 2011 at 19:39
  • so "bar tab" would convert to "BAr's tAB" ? Commented Dec 12, 2011 at 19:44
  • Very confusing question. There is a strtoupper($str) for the conversion, and a replacement for substituting spaces for any char. Commented Dec 12, 2011 at 19:53
  • Would "abb aba abc aba" = "abb aba's abc aba's" or "abb aba abc aba's"? Commented Dec 12, 2011 at 19:53
  • "ABB's ABA's ABc's ABA" I suspect. Or, if the "'s" is only added after an "a", then "ABB ABA's ABc ABA". Yes, highly unclear. Commented Dec 12, 2011 at 19:55

5 Answers 5

1

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 
Sign up to request clarification or add additional context in comments.

Comments

0

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

?

Comments

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

Comments

0

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 

Comments

0
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.