当前位置:编程学习 > php >>

php程序员进

function htmlcode($content){ $content=str_replace("\n","<br>",str_replace(" "," ",$content)); return $content; }这个函数能帮我写成面向对象的模式吗?我学习面向对象用谢谢。
答案:class htmlcode {

    public $content;

    public function __construct($content = '') {
        $this->content = $content;
    }

    public function replace($str, $tostr) {
        $this->content = str_replace($str, $tostr, $this->content);
    }

    public function getNewContent() {
        return $this->content;
    }

}

//实例化
$html = new htmlcode('asdfasad dfasdf<br>adsfdf df df');
$html->replace("<br>", "\n");
$html->replace("f", "F");
echo $html->getNewContent(); 

//或
$html = new htmlcode();
$html->content = 'asdfasad dfasdf<br>adsfdf df df';
$html->replace("<br>", "\n");
$html->replace("f", "F");
echo $html->getNewContent();
其他:class str{

 public function htmlcode($content=''){
 $content=str_replace("\n","<br>",str_replace(" "," ",$content));
  return $content;
 }

}

调用:
 $str=new str;
 $abc=$str->htmlcode('<div></div>'); 

上一个:编写程序由表单递交运算数与运算符,输出运算式与运算结果,用php编写。
下一个:我想学习网页制作,现在的情况是,会写C++程序,但是对html,php这类的一窍不通。请问该从哪里先学起?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,