shi*_*hin 3 php codeigniter snoopy
以下代码来自http://d.hatena.ne.jp/dix3/20081002/1222899116,代码运行良好.
这是在codeigniter 中使用snoopy的一个例子.
Q1.我是否正确地说我不能使用,
$this -> load -> library('snoopy')
Run Code Online (Sandbox Code Playgroud)
因为Snoopy.php不会创建对象.以下示例是如何做到的?如果是这样,你可以向我解释/指导如何详细说明的教程或解释吗?
if ( ! class_exists('Snoopy'))
{
require_once(APPPATH.'libraries/Snoopy'.EXT);
}
Run Code Online (Sandbox Code Playgroud)
Q2.作者为何使用
$to_specialchars=true
Run Code Online (Sandbox Code Playgroud)
它需要吗?
Q3.你能解释一下APPPATH和EXT吗?
APPPATH.'libraries/Snoopy'.EXT
Run Code Online (Sandbox Code Playgroud)
我在php.net中检查过但我找不到它.EXT必须是扩展名,但我可以在任何地方使用吗?
提前致谢.
我在application/library/Snoopy.php中有一个史努比
我有application/library/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 */
?>
Run Code Online (Sandbox Code Playgroud)
我有一个控制器应用程序/ controller/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 );
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个视图,application/view/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>
Run Code Online (Sandbox Code Playgroud)
Mat*_*ati 16
Q1.您可以使用:
$this->load->library('snoopy');
Run Code Online (Sandbox Code Playgroud)
在您的控制器中.并创建一个新的实例,如下所示:
$snooper = new Snoopy();
Run Code Online (Sandbox Code Playgroud)
他们使用的原因:
if (!class_exists('Snoopy')) {
require_once(APPPATH.'libraries/Snoopy'.EXT);
}
Run Code Online (Sandbox Code Playgroud)
因为如果你尝试使用$ this-> load-> library()会导致致命错误,因为库中没有加载器类.你可以在一个控制器中调用它是因为你的控制器扩展了控制器类,它扩展了ci_base类,扩展了ci_loader类,这是调用$ this-> load之类的函数的来源.你在这里展示的Scraping课没有.如果你深入挖掘,你会发现加载器基本上使用include_once来包含你正在尝试使用的任何类,助手等.
Q2.
$to_specialchars = true
Run Code Online (Sandbox Code Playgroud)
正在将一对函数声明用作参数.设置'= true'只是设置默认值,所以你可以这样做:
echo $scrappy->getWebHtml('http://example.com');
Run Code Online (Sandbox Code Playgroud)
与此相同:
echo $scrappy->getWebHtml('http://example.com', true);
Run Code Online (Sandbox Code Playgroud)
如果查看该函数的return语句,您将看到它们是$ to_specialchars正在被检查,如果它是真的,那么输出首先通过PHP函数htmlspecialchars()运行.
Q3.如果你查看codeigniter项目的根,在index.php中你会看到EXT定义为:
define('EXT', '.'.pathinfo(__FILE__, PATHINFO_EXTENSION));
Run Code Online (Sandbox Code Playgroud)
和APPATH:
if (is_dir($application_folder))
{
define('APPPATH', $application_folder.'/');
}
else
{
if ($application_folder == '')
{
$application_folder = 'application';
}
define('APPPATH', BASEPATH.$application_folder.'/');
}
Run Code Online (Sandbox Code Playgroud)
所以这些是在引导时设置的两个常量,因此您可以在应用程序中使用它们,如果您要更改它们,那么它就不会像您在所提供的代码中看到的那样使用它们.
请下次每个stackoverflow问题有一个问题:)