다음 코드는 http://d.hatena.ne.jp/dix3/20081002/1222899116이고 코드는 잘 작동합니다.Codeigniter의 클래스에서 객체 만들기
다음은 codeigniter에 snoopy을 사용하는 예입니다.
Q1. 사용할 수 없다고 말하면됩니다.
$this -> load -> library('snoopy')
Snoopy.php가 개체를 만들지 않아서입니다. 아래의 예가이를 수행하는 방법입니다. 그렇다면 자세히 설명하는 지침서 나 설명을 나에게 직접 설명하거나 지시 할 수 있습니까?
if (! class_exists('Snoopy'))
{
require_once(APPPATH.'libraries/Snoopy'.EXT);
}
질문 2. 작성자가 사용하는 이유
$to_specialchars=true
이 것이 필요합니까?
3. APPPATH와 EXT를 설명해 주시겠습니까?
APPPATH.'libraries/Snoopy'.EXT
php.net에서 확인했지만 찾을 수 없습니다. EXT는 확장이되어야하지만 어디서나 사용할 수 있습니까? 사전에
감사합니다.
나는 응용 프로그램/라이브러리에 스누피가/Snoopy.php
나는이 응용 프로그램/라이브러리/Snoopy.php
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Scraping{
var $c;
function Scraping(){
if (! class_exists('Snoopy'))
{
require_once(APPPATH.'libraries/Snoopy'.EXT);
}
$this -> c = new Snoopy();
}
function getWebHtml($url="",$to_specialchars=true){
$this ->c -> fetch($url);
$str = mb_convert_encoding((string) $this -> c -> results,"UTF-8","auto");
return ($to_specialchars) ? htmlspecialchars($str , ENT_QUOTES , "UTF-8") : $str ;
}
function getWebText($url="",$to_specialchars=true){
$this -> c -> fetchtext($url);
$str = mb_convert_encoding((string) $this -> c -> results,"UTF-8","auto");
return ($to_specialchars) ? htmlspecialchars($str , ENT_QUOTES , "UTF-8") : $str ;
}
function getWebLinks($url=""){
$this -> c -> fetchlinks($url);
return (array) $this-> c -> results ;
}
function getWebLinksText($url="",$delimiter="<br>"){
$arr = $this-> getWebLinks($url) ;
$ret ="";
foreach($arr as $k => $v){
$ret .= $v . $delimiter ;
}
return $ret;
}
} //endofclass
/* End of file Scraping.php */
/* Location: ./application/libraries/Scraping.php */
?>
내가 가진 컨트롤러 응용 프로그램/컨트롤러/mytasklist.php
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Mytasklist extends Controller {
function Mytasklist()
{
parent :: Controller();
$this -> load -> helper('url');
}
function index()
{
$data = "";
$this -> _SetTpl($data);
}
function _SetTpl($data)
{
$this -> load -> library("scraping");
$data["scraping"]["text"] = $this-> scraping -> getWebText("http://www.example.com/");
$data["scraping"]["html"] = $this-> scraping -> getWebHtml("http://www.example.com/");
$data["scraping"]["link"] = $this-> scraping -> getWebLinksText("http://www.example.com/","\n");
$tpl["page_title"] = "Welcome";
$tpl["main_content"] = $this -> load -> view('tasklist_view', $data , true);
$this -> load -> view('base_view', $tpl);
}
}
그리고 내가보기, 응용 프로그램 /보기/base_view.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta name="keywords" content="keyword here" />
<meta name="description" content="description here" />
<title><?php if(isset($page_title)){echo $page_title ;}?></title>
<?php if(isset($xajax_js)){echo $xajax_js ;}?>
<link href="http://127.0.0.1/ci_day4/css/mystyle.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="container">
<div id="rightblock">
<div id="content">
<?=$main_content?>
</div>
</div>
</div>
</body>
</html>
가
"codeigniter와 다른 라이브러리에서이 코드를 사용하고있는 이유는 무엇입니까?"어떻게 알았습니까? – shin
Q1에서 설명한 내용을 정리하려고했는데, 문제는 코드를 읽고 시도하는 것입니다. 한 번에 한 가지 질문 만하면 한 가지 거대한 대답보다 철저한 대답을하는 것이 더 쉽습니다. 희망이 도움이 되었기를 바랍니다. –