如何声明一个类函数来在一个句子的第一个单词中反转字母

时间:2010-12-22 18:59:36

标签: php oop

我已经声明了一个反转整个句子的函数:

<?php
class String {

    private $error;
    private $length;
    private $string = "";
    private $array = array();
    private $index = 0;
    private $indexOf = false;

    private $word;
    private $str;


    public function __construct($string="") {
        $this->string = $string;

    }

    public function revers_sentence() {
        $this->string = strrev($this->string);
        return $this;
    } 

...但是如何声明一个类函数来反转一个句子的第一个单词中的字母?

public function revers_1_word() {
    hmm ?
} 

2 个答案:

答案 0 :(得分:2)

为此声明方法如下:

public function reverse_first_word() {
    // Insert your implementation here.
}

: - )

答案 1 :(得分:1)

 public function reverse_1_word() {
   $str_words = explode(" ", $this->string);
   $str_words[0] = strrev($str_words[0]);
   $this->string = implode(" ", $str_words);
   return $this;
 }