2010-12-22 9 views
0

我已經聲明瞭一個函數來REVERS全句:如何聲明一個類的函數來扭轉信在一個句子的第一個字

<?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 ? 
} 
+0

你問我們寫的代碼對你來說,還是? – 2010-12-22 19:02:15

回答

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; 
} 
2

你最好聲明的方法爲這個如下:

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

:-)

+0

是的你的權利 - 對不起 – easyrider 2010-12-22 20:11:15

+0

@easyrider - 不用擔心,我只是有一點樂趣。順便說一句,如果你將pjabang的答案標記爲接受(點擊答案左側的灰色勾號),你和pjabang都將獲得聲望點。 (Stack Overflow的有效「貨幣」。) – 2010-12-22 21:02:45

相關問題