php存储例程、存储过程入门教程
php教程存储例程、存储过程入门教程
什么是存储例程?
存储例程是存储在数据库教程服务器中的一组sql语句,通过在查询中调用一个指定的名称来执行这些sql语句命令。
为什么要使用存储过程?
我们都知道应用程序分为两种,一种是基于web,一种是基于桌面,他们都和数据库进行交互来完成数据的存取工作。假设现在有一种应用程序包含了这两种,现在要修改其中的一个查询sql语句,那么我们可能要同时修改他们中对应的查询sql语句,当我们的应用程序很庞大很复杂的时候问题就出现这,不易维护!另外把sql查询语句放在我们的web程序或桌面中很容易遭到sql注入的破坏。而存储例程正好可以帮我们解决这些问题。
存储过程(stored procedure)、存储例程(store routine)、存储函数区别
Mysql存储例程实际包含了存储过程和存储函数,它们被统称为存储例程。
其中存储过程主要完成在获取记录或插入记录或更新记录或删除记录,即完成select insert delete update等的工作。而存储函数只完成查询的工作,可接受输入参数并返回一个结果。
创建存储过程、存储函数
create procedure 存储过程名(参数)
存储过程体
create function 存储函数名(参数)
存储函数体
假设现在有一个数据库omcmc中的表db_info 表结构如下:
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for db_news
-- ----------------------------
DROP TABLE IF EXISTS `db_news`;
CREATE TABLE `db_news` (
`id` int(10) NOT NULL auto_increment,
`title` varchar(200) NOT NULL,
`editor` varchar(20) default NULL,
`origin` varchar(20) default NULL,
`tags` varchar(200) default NULL,
`content` text NOT NULL,
`hits` int(10) default '0',
`ip` varchar(15) NOT NULL,
`time` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `db_news` VALUES ('1', First App', 'xqbar.com', 'xqbar.com', 'omcmc', 'detail。。', '100', '127.0.0.1', '1215051225');
使用上面的表我们创建一个简单的存储过程
create procedure select_news()
select title,hits from db_news;
在终端运行并调用
从上面的截图我们看到如何调用我们的存储过程
调用存储过程
call 存储过程名();
以上我们创建了一个简单的存储过程,当然我们的应用程序不可能使用这么简单的存储过程,我们需要的是能给存储过程传递参数,以返回给我们所需要的结果数据。下面就了解下存储过程的参数。
存储过程的参数
通常存储过程接受用户的参数,返回结果给调用用户。
mysql教程规定对于存储过程的参数要求其每个参数都必须声明其参数名,数据类型以及该参数是输入参数还是用于返回信息还是两者兼有,对于存储函数php只支持输入参数。
声明参数时规定要使用关键字IN,OUT,INOUT。
其中:
IN:用于输入参数
OUT:用于返回参数
INOUT:用于向存储过程传递参数值,如果该值改变则返回
另外规定对于声名为OUT,INOUT的参数当我们调用存储过程时需要在参数名前加@,以确保参数在过程外调用,下面我们修改上面的存储过程以传递信息编号给存储过程select_news,返回对应的信息标题给我们查看。
drop procedure if exists select_news;
create procedure select_news(IN id int,OUT title varchar(200))
select db_news.title from db_news where db_news.id=id;
在终端运行并调用
注意调用格式
call select_news(1,@title);
其中1是我们要传入的信息编号,title是要返回的对应信息编号标题,由于是OUT所以再调用时要在其前加@在过程外调用。(见下例)
drop procedure if exists getNum;
create procedure getNum(OUT num int)
select 100 into num;
call getNum(@num);
select @num;
截图:
php存储例程、存储过程进阶学习之二
接着上篇文章,再调用有返回值时上个例子我们可以使用以下的方法调用存储过程:
select @返回参数;
我们现在使用这个方法来调用上面我们创建的select_news,来看看
有人会疑问为什么返回的title是null,而不是像call select_news(1,@title)一样返回的是我们数据库编号对应的数据First App,这是由于我们的存储过程体并没有给title返回参数返回值。
下面我们来看看存储函数
仍然以db_news表为例说明,不过我们在这个存储函数中我们加入其他一些关于存储例程的知识来引入我们这次要谈及的学习对象。
drop function if exists count_news;
delimiter //
create function count_news(hits int) returns int
comment '根据传入的点击次数统计超过此点击数的信息数目'
begin
declare total_news int;
declare hits_num int default 0;
if hits>=0 then
set hits_num=hits;
select count(id) into total_news from db_news where db_news.hits>hits_num;
else
set total_news=0;
end if;
return total_news;
end;
//
delimiter ;
在上面的存储函数中我们使用到了存储例程变量的声明、设置其值、判断语句、存储函数特有的返回值类型及存储例程如何描述(comment).
与存储过程返回参数不同的是存储函数在定义时没用直接声明哪个变量是返回参数,而只是使用了returns声明了返回参数所属的数据类型,返回参数是在函数体中使用return返回要返回的数据变量的形式来表示的。这就需要注意的是:
存储函数只支持输入参数,并且输入参数前没有IN或INOUT.
返回上面的存储函数,我们来解释下代码含义:
drop function if exists count_news;
如果存在存储函数count_news则删除该存储函数,与存储过程一样我们可以使用下面的语句删除存储过程或函数
drop procedure|function [if exists] 存储过程名|存储函数名;
delimiter //
使用delimiter更改mysql默认使用分号(;)使用新的结束符号来结束当前语句,使用delimiter后原先默认的分号(;)结束语句符号不再起作用,直到重新恢复声明结束符后。
create function count_news(hits int) returns int
注意存储函数的参数只有输入参数并且前不再声明IN或INOUT,返回只需声明要返回的数据类型
comment '根据传入的点击次数统计超过此点击数的信息数目'
使用comment 来描述该存储过程或存储函数的功能信息。使用格式为 comment '描述字符串 '
begin
使用begin限定一个处理模块
declare total_news int;
声明变量 格式 declare 变量名 数据类型 [default 默认值]
declare hits_num int default 0;
声明变量 格式 declare 变量名 数据类型 [default 默认值]
if hits>=0 then
set hits_num=hits;
使用set 给变量赋值
select count(id) into total_news from db_news where db_news.hits>hits_num;
sql语句可以使用into给变量赋值
else
set total_news=0;
end if;
上面是一个判断语句,注意格式为if-else-end if 或者 if –else if-else-end if
注意分号(;)
return total_news;
使用return 返回存储函数要返回的值,注意该值只能是一个值。
end;
//
delimiter ;
综合一下我们上面的概念知识:
1. 存储函数的参数与存储过程有何不同。
2. 在存储例程中如何更改mysql的默认结束符号。
3. 在存储例程中如何描述功能信息。
4. 声明、设置变量。
5. begin-end语句块。
6. 条件判断if-else if-else-end if 或if-else-end if 或者if-end if
php教程lot生成图片类详解
我个人使用的是phplot,它是一个利用php的图象函数编写的一个自动生成类,首先申明我对他也只是了解. 在原来的有些版本好象还需要多方面的配置和支持,不过现在都用的是php5系列了,大家只要了解一些常用到的函数,就比如我们学习一种软件一样,只需要懂得怎样使用他就可以了,如果感兴趣的朋友可以深入的学习学习,不过对于我们大多数的朋友来说,只要会使用他就可以了,毕竟也不是常用,只是需要用php作图的时候才用到他。所以我们只需要知道他怎么用就可以了,那么就
必须知道他的函数的作用,好比我们学习discuz一样,要懂得怎么用他就可以了!
phplot的下载地址是http://www.sourceforge.net/projects/phplot/现在最新版本好像是phplot5.0
他的常用函数分为几个类:配置函数,显示函数,颜色函数
1.配置函数:配置phplot使用什么样的类型,以及以什么样的方式显示图象。
a.SetDataType($which_dt):设定使用的数据类型,在这个里面可以使用多种类型。
(1)text-date:数据沿着x柱,等间距排行。每个数组元素代表x轴上的某一位置的点,他同时也是数组,第一个元素代表x坐标,后面
的所有元素都代表y坐标。
(2) data-data:与上面的类型相似,不同的是数值数组的第二个元素代表x坐标,后面的代表y坐标,而第一个元素仅仅是一个标签。
(3)data-data-error:与data-data类似,不同的是他的数值数组后面还有两个元素代表error_plus和error_minus,如
(data_labl,x_position,y_position,error_plus,error_minus).
b.SetDataValues($which_dv):把一个数组$which_dv赋给类的一个变量$this->data_values.这个函数要开始作图之前调用。
c.SetPlotType($which_pt):设定图表的类型,可以是bars,lines,linepoints,area,points,pie等。
d.SetErrorBarLineWidth($wd):设定错误栏的宽度。
e.SetFileFormat($which_file_format):设定输出的图象文件的格式,可以是GIF,PNG,JPEG等。还有要看你的GD库是否支持。
f.SetUseTTF($which_ttf):设定是否使用TTF,如果编译php支持TTF,就使用SetUseTTF("1");否则设置为0.
2.显示函数:显示图象设定输出图表所使用的线条的类型,宽度等参量,还可以设定坐标轴刻度的间距,图表的大小等。
a.SetErrorBarShape($which_ebs):设定精度线的类型,可以是line或者是tee,如果是tee,则T型线的半度设定为SetErrorBarSize.
b.SetErrprBarSize($which_ebs):设置精度线的宽度。
c.SetHorizTickIncreament($which_ti):设定x轴的显示刻度的间距。
d.SetHorizTicks($whick_nt):设定x轴显示刻度的个数。(不能和SetHorizTickIncreament)
e.SetNumVertTicks($which_nt):设定x轴显示刻度的个数。(不能和SetVertTickIncreament)
f.SetPlotArearpixels($x1,$y1,$x2,$y2):设定图表大小。
g.SetPointShape($which_pt):设置定点的形状:rect,circle,diamond,triangle,dot,line,halfline.
h.SetPointSize($whick_ps教程):设定点的宽度。
i.SetPrecisionX($whick_prec):设定x轴的精度。$whick_prec代表小数点后面数字的个数。
j.SetPrecisiony($whick_prec)设定y轴的精度。$whick_prec代表小数点后面数字的个数。
k.SetSjading($whick_s):设定阴影的宽度。
l.SetTickLength($which_tl):设定坐标轴上的标记线的长度,单位为pixel.
m.SetTile($title):设定图表的标题。
n.SetVertTickIncreament($whick_ti):和SetHorizTicks($whick_nt)这两个函数用于设定坐标轴上标记线的垂直和水平间隔。
o.SetXDataLabelMaxlength($which_xdlm):设定x轴上的标签的最大长度。
p.SetXGridLabelType($which_xtf):设定x轴的标签类型,可以是time,title,data,none或者default.
(1).time:由函数strftime()设定。
(2).title:文本类型。
(3).data:用函数number_format()格式化数字。
(4).none:没有任何标签。
(5).default:按照输入的形式输出.
3.颜色函数:颜色函数用于设定图表中的各元素的显示颜色,包括图象背景颜色,删格线的颜色,标题颜色等!
a.SetBackgroundColor($which_color):设定整个图象的背景颜色。
b.SetGridColor($which_color):设定删格线的颜色。
c.SetLegend($which_legend):参数是一个文本数组,他的内容显示在一个图列框中。
d.SetLegendPixels($which_x,$which_y,$which_type):设定图列框左下角点的坐标,最后一个参数以后可用。
e.SetLightGridColor($which_color):删割线线有两种颜色,这个函数设定其中的一种。
f.SetLineWidth($which_lt):设定图表中使用的线宽,他也影响精度线的宽度。
g.SetLineStyles($which_sls):设定线条的类型,可以是实线或者虚线。
h.SetPlotBgColor($which_color):设定使用SetPlotAreaPixels()函数设定的区域的颜色。
i.SetTextColor($which_color):设定文本的颜色,默认是黑色。
j.SetTickColor($which_color):设定坐标轴上刻线的颜色。
k.SetTitleColor($which_color):设定标题颜色。
看个实例
生成上面图形的代码如下:
# PHPlot Demo
# 2008-01-09 ljb
# For more information see http://sourceforge.net/projects/phplot/
# Load the PHPlot class library:
require_once 'phplot.php';
# Define the data array: Label, the 3 data sets.
# Year, Features, Bugs, Happy Users:
$data = array(
array('2001', 60, 35, 20),
array('2002', 65, 30, 30),
array('2003', 70, 25, 40),
array('2004', 72, 20, 60),
array('2005', 75, 15, 70),
array('2006', 77, 10, 80),
array('2007', 80, 5, 90),
);
# Create a PHPlot object which will make a 600x400 pixel image:
$p = new PHPlot(600, 400);
# Use TrueType fonts:
$p->SetDefaultTTFont('./arial.ttf');
# Set the main plot title:
$p->SetTitle('PHPlot Customer Satisfaction (estimated)');
# Select the data array representation and store the data:
$p->SetDataType('text-data');
$p->SetDataValues($data);
# Select the plot type - bar chart:
$p->SetPlotType('bars');
# Define the data range. PHPlot can do this automatically, but not as well.
$p->SetPlotAreaWorld(0, 0, 7, 100);
# Select an overall image background color and another color under the plot:
$p->SetBackgroundColor('#ffffcc');
$p->SetDrawPlotAreaBackground(True);
$p->SetPlotBgColor('#ffffff');
# Draw lines on all 4 sides of the plot:
$p->SetPlotBorderType('full');
# Set a 3 line legend, and position it in the upper left corner:
$p->SetLegend(array('Features', 'Bugs', 'Happy Users'));
$p->SetLegendWorld(0.1, 95);
# Turn data labels on, and all ticks and tick labels off:
$p->SetXDataLabelPos('plotdown');
$p->SetXTickPos('none');
$p->SetXTickLabelPos('none');
$p->SetYTickPos('none');
$p->SetYTickLabelPos('none');
# Generate and output the graph now:
$p->DrawGraph();
怎么样,不错吧。。 喜欢的朋友可以到 http://phplot.sourceforge.net/ 官方站下载
php教程 SOAP WSDL简单应用实例
<?php
$ws = "http://www.111cn.net/sd/2001/TemperatureService.wsdl";
$zipcode = "23590";
$client = new SoapClient($ws,array('trace' => 1));
$temperature = $client->getTemp($zipcode);
echo htmlspecialchars($client->__getLastRequest());
?>
获取soap发送的数据
<?php
$ws = "http://www.111cn.net/sd/2001/TemperatureService.wsdl";
$zipcode = "12312";
$client = new SoapClient($ws,array('trace' => 1));
$temperature = $client->getTemp($zipcode);
echo htmlspecialchars($client->__getLastResponse());
?>
wdsl应用
<?php
$ws = "http://www.111cn.net/sd/2001/TemperatureService.wsdl";
$client = new SoapClient($ws);
var_dump($client->__getFunctions());
?>
php教程 多风格的分页类(支持ajax分页)
<?
/*** example:
* 模式四种分页模式:
require_once('../libs/classes/page.class.php');
$page=new page(array('total'=>1000,'perpage'=>20));
echo 'mode:1<br>'.$page->show();
echo '<hr>mode:2<br>'.$page->show(2);
echo '<hr>mode:3<br>'.$page->show(3);
echo '<hr>mode:4<br>'.$page->show(4);
开启AJAX:
$ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
echo 'mode:1<br>'.$ajaxpage->show();
采用继承自定义分页显示模式:*/
class page
{
/**
* config ,public
*/
var $page_name="PB_page";//page标签,用来控制url页
var $next_page='>';//下一页
var $pre_page='<';//上一页
var $first_page='First';//首页
var $last_page='Last';//尾页
var $pre_bar='<<';//上一分页条
var $next_bar='>>';//下一分页条
var $format_left='[';
var $format_right=']';
var $is_ajax=false;//是否支持AJAX分页模式/**
* private
*
*/
var $pagebarnum=10;//控制记录条的个数。
var $totalpage=0;//总页数
var $ajax_action_name='';//AJAX动作名
var $nowindex=1;//当前页
var $url="";//url地址头
var $offset=0;/**
* constructor构造函数
*
* @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']
*/
function page($array)
{
if(is_array($array)){
if(!array_key_exists('total',$array))$this->error(__FUNCTION__,'need a paramof total');
$total=intval($array['total']);
$perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;
$nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';
$url=(array_key_exists('url',$array))?$array['url']:'';
}else{
$total=$array;
$perpage=10;
$nowindex='';
$url='';
}
if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.' is not apositive integer!');
if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' is nota positive integer!');
if(!empty($array['page_name']))$this->set('page_name',$array['page_name']);//设置pagename
$this->_set_nowindex($nowindex);//设置当前页
$this->_set_url($url);//设置链接地址
$this->totalpage=ceil($total/$perpage);
$this->offset=($this->nowindex-1)*$perpage;
if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//打开AJAX模式
}
/**
* 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception
*
* @param string $var
* @param string $value
*/
function set($var,$value)
{
if(in_array($var,get_object_vars($this)))
$this->$var=$value;
else {
$this->error(__FUNCTION__,$var." does not belong to PB_Page!");
}}
/**
* 打开倒AJAX模式
*
* @param string $action 默认ajax触发的动作。
*/
function open_ajax($action)
{
$this->is_ajax=true;
$this->ajax_action_name=$action;
}
/**
* 获取显示"下一页"的代码
*
* @param string $style
* @return string
*/
function next_page($style='')
{
if($this->nowindex<$this->totalpage){
return $this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style);
}
return '<span class=".$style.">'.$this->next_page.'</span>';
}/**
* 获取显示“上一页”的代码
*
* @param string $style
* @return string
*/
function pre_page($style='')
{
if($this->nowindex>1){
return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
}
return '<span class=".$style.">'.$this->pre_page.'</span>';
}/**
* 获取显示“首页”的代码
*
* @return string
*/
function first_page($style='')
{
if($this->nowindex==1){
return '<span class=".$style.">'.$this->first_page.'</span>';
}
return $this->_get_link($this->_get_url(1),$this->first_page,$style);
}/**
* 获取显示“尾页”的代码
*
* @return string
*/
function last_page($style='')
{
if($this->nowindex==$this->totalpage){
return '<span class=".$style.">'.$this->last_page.'</span>';
}
return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
}function nowbar($style='',$nowindex_style='')
{
$plus=ceil($this->pagebarnum/2);
if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
$begin=$this->nowindex-$plus+1;
$begin=($begin>=1)?$begin:1;
$return='';
for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
{
if($i<=$this->totalpage){
if($i!=$this->nowindex)
$return.=$this->_get_text($this->_get_link($this->_get_url($i),$i,$style));
else
$return.=$this->_get_text('<spanclass=".$nowindex_style.">'.$i.'</span>');
}else{
break;
}
$return.="n";
}
unset($begin);
return $return;
}
/**
* 获取显示跳转按钮的代码
*
* @return string
*/
function select()
{
$return='<select name="PB_Page_Select">';
for($i=1;$i<=$this->totalpage;$i++)
{
if($i==$this->nowindex){
$return.='<option value="'.$i.'" selected>'.$i.'</option>';
}else{
$return.='<option value="'.$i.'">'.$i.'</option>';
}
}
unset($i);
$return.='</select>';
return $return;
}/**
* 获取mysql教程 语句中limit需要的值
*
* @return string
*/
function offset()
{
return $this->offset;
}/**
* 控制分页显示风格
*
* @param int $mode
* @return string
*/
function show($mode=1)
{
switch ($mode)
{
case '1':
$this->next_page='下一页';
$this->pre_page='上一页';
return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
break;
case '2':
$this->next_page='下一页';
$this->pre_page='上一页';
$this->first_page='首页';
$this->last_page='尾页';
return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';
break;
case '3':
$this->next_page='下一页';
$this->pre_page='上一页';
$this->first_page='首页';
$this->last_page='尾页';
return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
break;
case '4':
$this->next_page='下一页';
$this->pre_page='上一页';
return $this->pre_page().$this->nowbar().$this->next_page();
break;
case '5':
return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
break;
}}
/*----------------private function (私有方法)-----------------------------------------------------------*/
/**
* 设置url头地址
* @param: String $url
* @return boolean
*/
function _set_url($url="")
{
if(!empty($url)){
//手动设置
$this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";
}else{
//自动获取
if(empty($_SERVER['QUERY_STRING'])){
//不存在QUERY_STRING时
$this->url=$_SERVER['REQUEST_URI']."?".$this->page_name."=";
}else{
//
if(stristr($_SERVER['QUERY_STRING'],$this->page_name.'=')){
//地址存在页面参数
$this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_SERVER['REQUEST_URI']);
$last=$this->url[strlen($this->url)-1];
if($last=='?'||$last=='&'){
$this->url.=$this->page_name."=";
}else{
$this->url.='&'.$this->page_name."=";
}
}else{
//
$this->url=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
}//end if
}//end if
}//end if
}/**
* 设置当前页面
*
*/
function _set_nowindex($nowindex)
{
if(empty($nowindex)){
//系统获取if(isset($_GET[$this->page_name])){
$this->nowindex=intval($_GET[$this->page_name]);
}
}else{
//手动设置
$this->nowindex=intval($nowindex);
}
}/**
* 为指定的页面返回地址值
*
* @param int $pageno
* @return string $url
*/
function _get_url($pageno=1)
{
return $this->url.$pageno;
}/**
* 获取分页显示文字,比如说默认情况下_get_text('<a href="">1</a>')将返回[<ahref="">1</a>]
*
* @param String $str
* @return string $url
*/
function _get_text($str)
{
return $this->format_left.$str.$this->format_right;
}/**
* 获取链接地址
*/
function _get_link($url,$text,$style=''){
$style=(empty($style))?'':'class="'.$style.'"';
if($this->is_ajax){
//如果是使用AJAX模式
return '<a '.$style.' href="网页特效:'.$this->ajax_action_name.'(''.$url.'')">'.$text.'</a>';
}else{
return '<a '.$style.' href="'.$url.'">'.$text.'</a>';
}
}
/**
* 出错处理方式
*/
function error($function,$errormsg)
{
die('Error in file <b>'.__FILE__.'</b> ,Function <b>'.$function.'()</b>:'.$errormsg);
}
}
$page=new page(array('total'=>1000,'perpage'=>20));
echo 'mode:1<br>'.$page->show();
echo '<hr>mode:2<br>'.$page->show(2);
echo '<hr>mode:3<br>'.$page->show(3);
echo '<hr>mode:4<br>'.$page->show(4);
echo '<hr>开始AJAX模式:';
$ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test'));
echo 'mode:1<br>'.$ajaxpage->show();
?>
中文搜索引擎来说, 中文分词是整个系统最基础的部分之一, 因为目前基于单字的中文搜索算法并不是太好. 当然, 本文不是要对中文搜索引擎做研究, 而是分享如果用 PHP 做一个站内搜索引擎. 本文是这个系统中的一篇
进行中文分词的 PHP 类就在下面了, 用 proc_open() 函数来执行分词程序, 并通过管道和其交互, 输入要进行分词的文本, 读取分词结果.
<?php教程
class NLP{
private static $cmd_path;// 不以'/'结尾
static function set_cmd_path($path){
self::$cmd_path = $path;
}private function cmd($str){
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
);
$cmd = self::$cmd_path . "/ictclas";
$process = proc_open($cmd, $descriptorspec, $pipes);if (is_resource($process)) {
$str = iconv('utf-8', 'gbk', $str);
fwrite($pipes[0], $str);
$output = stream_get_contents($pipes[1]);fclose($pipes[0]);
fclose($pipes[1]);$return_value = proc_close($process);
}/*
$cmd = "printf '$input' | " . self::$cmd_path . "/ictclas";
exec($cmd, $output, $ret);
$output = join("n", $output);
*/$output = trim($output);
$output = iconv('gbk', 'utf-8', $output);return $output;
}/**
* 进行分词, 返回词语列表.
*/
function tokenize($str){
$tokens = array();$output = self::cmd($input);
if($output){
$ps教程 = preg_split('/s+/', $output);
foreach($ps as $p){
list($seg, $tag) = explode('/', $p);
$item = array(
'seg' => $seg,
'tag' => $tag,
);
$tokens[] = $item;
}
}return $tokens;
}
}
NLP::set_cmd_path(dirname(__FILE__));
?>
使用起来很简单(确保 ICTCLAS 编译后的可执行文件和词典在当前目录):<?php
require_once('NLP.php');
var_dump(NLP::tokenize('你好啊, 世界!'));
?>
站长经验,
如果想做到搜索引擎分词,需要强大的词库及更智能化的汉语拼音以及写法,习惯等功能库。
相关文章
- Create Procedure AtoC @ChangeMoney Money as Set Nocount ON Declare @String1 char(20) Declare @String2 char(30) ...2016-11-25
- 存储过程在数据库的应用中我们用到的非常的多了,下面我们来看一篇关于PHP操作MSSQL存储过程修改用户密码的例子,具体的如下所示。 mssql2008 存储过程 下面可以直接...2016-11-25
- 轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
- 这篇文章算是超级入门级别的了,我们下面来给各位介绍在photoshop画斜线/直线/虚线的教程了,希望下面这篇文章给你入门来帮助。 PS怎么画斜线 选择铅笔工具,或者画笔...2016-09-14
- 这篇文章主要给大家介绍的是关于Lua语言新手入门的简单教程,文中通过示例代码一步步介绍的非常详细,对各位新手们的入门提供了一个很方便的教程,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。...2020-06-30
- 具体详情请看下文小编给大家带来的知识点。同编写程序类似,存储过程中也有对应的条件判断,功能类似于if、switch。在MySql里面对应的是IF和CASE1、IF判断IF判断的格式是这样的:IF expression THEN commands [ELSEIF ex...2015-10-21
- 本文章来给大家详细介绍在php中如何来调用执行mysql存储过程然后返回由存储过程返回的值了,有需要了解的同学可进入参考。 。调用存储过程的方法。 a。如果存储过...2016-11-25
- 这篇文章主要为大家分享了20分钟MySQL基础入门教程,快速掌握MySQL基础知识,真正了解MySQL,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-12-02
- 这篇文章主要介绍了C#线程入门教程之单线程介绍,本文讲解了什么是进程、什么是线程、什么是多线程等内容,并给出了一个单线程代码示例,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了react hooks入门详细教程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-04-06
- 这篇文章主要介绍了C#调用存储过程的方法,结合实例形式详细分析了各种常用的存储过程调用方法,包括带返回值、参数输入输出等,需要的朋友可以参考下...2020-06-25
- 复制代码 代码如下:call PROCEDURE_split('分享,代码,片段',',');select * from splittable;复制代码 代码如下:drop PROCEDURE if exists procedure_split;CREATE PROCEDURE `procedure_split`( inputstring varc...2014-05-31
- 这篇文章主要介绍了CocosCreator入门教程之用TS制作第一个游戏,对TypeScript感兴趣的同学,一定要看一下...2021-04-16
- 这篇文章主要介绍了详解Servlet入门级设置(超详细 IDEA2020版),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-04
- 这里主要讲到关于在php教程类的调用,申请明,使用以前各种类的实例方法与操作过程,让你清楚的知道php类的construct destruct clone call wake sleep用法。 简单购物车...2016-11-25
- 现在最热门的前端框架有AngularJS、React、Bootstrap等。自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我,下面来跟我一起领略ReactJs的风采吧~~ 文章有点长,耐心读完,你会有很大收获哦...2021-04-05
- c#调用存储过程实现登录界面详解...2020-06-25
- MySQL发展到今天,InnoDB引擎已经作为绝对的主力,除了像大数据量分析等比较特殊领域需求外,它适用于众多场景。然而,仍有不少开发者还在“执迷不悟”的使用MyISAM引擎,觉得对InnoDB无法把握好,还是MyISAM简单省事,还能支持快...2015-11-24
- 这篇文章主要介绍了阿里云ECS服务器入门使用流程(新手必看教程),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-07-13
- 这篇文章主要介绍了C#获取存储过程返回值的方法,大家参考使用吧...2020-06-25