例如搜索“ok”就会匹配ok里面的数组 “你好,good,很好\"打印出来如果搜索“好”没有匹配到第一数组,就会模糊搜索所有含有“好”的的关键词,例如“你好 很好 爱好”
如下面例子:
代码如下 |
复制代码 |
$foo[1]['a']['xx'] = 'bar 1';
$foo[1]['b']['xx'] = 'bar 2';
$foo[2]['a']['bb'] = 'bar 3';
$foo[2]['a']['yy'] = 'bar 4';
$foo[3]['c']['dd'] = 'bar 3';
$foo[3]['f']['gg'] = 'bar 3';
$foo['info'][1] = 'bar 5';
|
如果要查找 bar 3 怎么进行查找呢。有三个结果,而这三个结果都要,看下面的函数:
-------------------------------------------------------------------------------------------------------------------------------
代码如下 |
复制代码 |
function array_search_re($needle, $haystack, $a=0, $nodes_temp=array()){
global $nodes_found;
$a++;
foreach ($haystack as $key1=>$value1) {
$nodes_temp[$a] = $key1;
if (is_array($value1)){
array_search_re($needle, $value1, $a, $nodes_temp);
}
else if ($value1 === $needle){
$nodes_found[] = $nodes_temp;
}
}
return $nodes_found;
}
|
---------------------------------------------------------------------------------------------------------------------------------
这个函数就可以把上面要查找到的内容全部返回出键名来
代码如下 |
复制代码 |
$result = array_search_re('bar 3', $foo);
print_r($result);
|
输出结果为如下:
Array ( [0] => Array ( [1] => 2 [2] => a [3] => bb )
[1] => Array ( [1] => 3 [2] => c [3] => dd )
[2] => Array ( [1] => 3 [2] => f [3] => gg )
)
1 php搜索多维数组的键名
代码如下 |
复制代码 |
function array_search_key($needle, $haystack){
global $nodes_found;
foreach ($haystack as $key1=>$value1) {
if ($key1=== $needle){
$nodes_found[] = $value1;
}
if (is_array($value1)){
array_search_key($needle, $value1);
}
}
return $nodes_found;
}
$result = array_search_key('a', $foo);
print_r($result);
|
输出结果为如下:
代码如下 |
复制代码 |
Array
(
[0] => Array
(
[xx] => bar 1
)
[1] => Array
(
[bb] => bar 3
)
[2] => Array
(
[yy] => bar 4
)
)
|
类似全文匹配!用二个循环 第一个循环keys 第二个模糊匹配 用函数strstr() 来解决
本文章来详细的介绍一在我们php开发中常用的几种正则表达式,如有网址、email、手机号码正则表达代码
1. 判断Email:
域名由各国文字的特定字符集、英文字母、数字及“-”(即连字符或减号)任意组合而成, 但开头及结尾均不能含有“-”,“-”不能连续出现 。 域名中字母不分大小写。域名最长可达60个字节(包括后缀.com、.net、.org等)。
/^[a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?$/i;
/内容/i 构成一个不区分大小写的正则表达式;
基本规则如下
代码如下 |
复制代码 |
preg_match('/^[a-z0-9_-]+(.[_a-z0-9-]+)*@([_a-z0-9-]+.)+([a-z]{2}
|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/',$email) |
例1
代码如下 |
复制代码 |
<?php
function is_email($email){
return strlen($email) > 6 && preg_match(“/^[w-.]+@[w-]+(.w+)+$/“, $email);
}
?>
|
例2
代码如下 |
复制代码 |
<html>
<body>
<?php
$email_pattern = '/w{6,16}@w{1,}.w{2,3}/i';
$email_valid = 'test_123@126.net';
$email_invalid = 'test@test%@111@com';
$matches = array();
preg_match($email_pattern, $email_valid, $matches[]);
preg_match($email_pattern, $email_invalid, $matches[]);
var_dump($matches);
?>
</body>
</html>
结果
array(2) { [0]=> array(1) { [0]=> string(16) "test_123@126.net" } [1]=> array(0) { } }
|
2. 判断Url:
例1
代码如下 |
复制代码 |
function is_url($str){
return preg_match(“/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]’:+!]*([^<>"])*$/“, $str);
}
|
例2
php判断url地址并自动转换为超链接,在一段字符串中用正则表达式匹配出url,在将url转换为超链接,点击可访问地址
代码如下 |
复制代码 |
<?php
function autolink($foo)
{
$foo = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_/+.~#?&//=]+)', '<a href="/1" mce_href="/1" target=_blank rel=nofollow>/1</a>', $foo);
if( strpos($foo, "http") === FALSE ){
$foo = eregi_replace('(www.[-a-zA-Z0-9@:%_/+.~#?&//=]+)', '<a href="http:///1" mce_href="http:///1" target=_blank rel=nofollow >/1</a>', $foo);
}else{
$foo = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_/+.~#?&//=]+)', '/1<a href="http:///2" mce_href="http:///2" target=_blank rel=nofollow >/2</a>', $foo);
}
return $foo;
}
?>
|
3. 判断手机号码:
例1
代码如下 |
复制代码 |
function is_mobile($str){
return preg_match(“/^(((d{3}))|(d{3}-))?13d{9}$/“, $str);
}
例2
if(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/",$mobilephone)){
//验证通过
}else{
//手机号码格式不对
}
|
PHP分页器制作,自动生成分面页码,JS调用函数,分页的原理大致如下,分页程序有两个非常重要的参数:每页显示几条记录($pagesize)和当前是第几页($page)。
有了这两个参数就可以很方便的写出分页程序,我们以MySql数据库作为数据源,在mysql里如果要想取出表内某段特定内容可以使用的 T-SQL语句:select * from table limit offset,rows来实现。这里的offset是记录偏移量,它的计算方法是offset=$pagesize*($page-1),rows是要显示的记录条数,这里就是$page。也就是说select * from table limit 10,10这条语句的意思是取出表里从第11条记录开始的20条记录。
代码如下 |
复制代码 |
<?php
class PageView{
/**页码**/
public $pageNo = 1;
/**页大小**/
public $pageSize = 20;
/**共多少页**/
public $pageCount = 0;
/**总记录数**/
public $totalNum = 0;
/**偏移量,当前页起始行**/
public $offSet = 0;
/**每页数据**/
public $pageData = array();
/**是否有上一页**/
public $hasPrePage = true;
/**是否有下一页**/
public $hasNextPage = true;
public $pageNoList = array();
public $jsFunction ='jsFunction';
/**
*
* @param unknown_type $count 总行数
* @param unknown_type $size 分页大小
* @param unknown_type $string
*/
public function __construct($count=0, $size=20,$pageNo=1,$pageData =array(),$jsFunction='jsFunction'){
$this->totalNum = $count;//总记录数
$this->pageSize = $size;//每页大小
$this->pageNo = $pageNo;
//计算总页数
$this->pageCount = ceil($this->totalNum/$this->pageSize);
$this->pageCount = ($this->pageCount<=0)?1:$this->pageCount;
//检查pageNo
$this->pageNo = $this->pageNo == 0 ? 1 : $this->pageNo;
$this->pageNo = $this->pageNo > $this->pageCount? $this->pageCount : $this->pageNo;
//计算偏移
$this->offset = ( $this->pageNo - 1 ) * $this->pageSize;
//计算是否有上一页下一页
$this->hasPrePage = $this->pageNo == 1 ?false:true;
$this->hasNextPage = $this->pageNo >= $this->pageCount ?false:true;
$this->pageData = $pageData;
$this->jsFunction = $jsFunction;
}
/**
* 分页算法
* @return
*/
private function generatePageList(){
$pageList = array();
if($this->pageCount <= 9){
for($i=0;$i<$this->pageCount;$i++){
array_push($pageList,$i+1);
}
}else{
if($this->pageNo <= 4){
for($i=0;$i<5;$i++){
array_push($pageList,$i+1);
}
array_push($pageList,-1);
array_push($pageList,$this->pageCount);
}else if($this->pageNo > $this->pageCount - 4){
array_push($pageList,1);
array_push($pageList,-1);
for($i=5;$i>0;$i--){
array_push($pageList,$this->pageCount - $i+1);
}
}else if($this->pageNo > 4 && $this->pageNo <= $this->pageCount - 4){
array_push($pageList,1);
array_push($pageList,-1);
array_push($pageList,$this->pageNo -2);
array_push($pageList,$this->pageNo -1);
array_push($pageList,$this->pageNo);
array_push($pageList,$this->pageNo + 1);
array_push($pageList,$this->pageNo + 2);
array_push($pageList,-1);
array_push($pageList,$this->pageCount);
}
}
return $pageList;
}
/***
* 创建分页控件
* @param
* @return String
*/
public function echoPageAsDiv(){
$pageList = $this->generatePageList();
$pageString ="<div class='pagination'><div class='page-bottom'>";
if(!empty($pageList)){
if($this->pageCount >1){
if($this->hasPrePage){
$pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo-1) . ")">上一页</a>";
}
foreach ($pageList as $k=>$p){
if($this->pageNo == $p){
$pageString = $pageString ."<span class='page-cur'>" . $this->pageNo . "</span>";
continue;
}
if($p == -1){
$pageString = $pageString ."<span class='page-break'>...</span>";
continue;
}
$pageString = $pageString ."<a href="javascript:" .$this->jsFunction . "(" . $p . ")">" . $p . "</a>";
}
if($this->hasNextPage){
$pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo+1) . ")">下一页</a>";
}
}
}
$pageString = $pageString .("</div></div>");
return $pageString;
}
}
?>
|
css
代码如下 |
复制代码 |
<style type="text/css">
<!--
.pagination {font-family: Tahoma;overflow: hidden; padding-top: 12px; text-align: center;}
.pagination-tab { margin-bottom: 20px;}
.pagination a, .pagination .page-cur, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-break, .pagination .page-skip {
display: inline-block;font-family: Tahoma,SimSun,Arial; height: 22px;line-height:22px; margin: 0; min-width: 16px;padding: 0 5px; text-align: center; vertical-align: top; white-space: nowrap;}
.pagination a, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-cur, .pagination .page-break {
border: 1px solid #ed3d83; color:#e9357d; font-weight:bold;}
.pagination a:hover { border: 1px solid #ed3d83;text-decoration: none; background-color:#f95f9d; color:#fff;}
.pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g { width: 36px; background-image: url(../static/img/page.gif);}
.pagination .page-prev { background-position: -0px -38px; padding-left: 16px;}
.pagination .page-prev_g { background-position:0px -59px; padding-left: 16px; color:#cbcbcb; font-weight:normal;}
.pagination .page-next { background-position: 0px 0px; padding-right: 16px; font-weight:normal;}
.pagination .page-next_g { background-position: -0px -19px; padding-right: 16px; color:#cbcbcb;}
.pagination .page-cur {background-color: #f95f9d; border: 1px solid #ed3d83;color: #fff;font-weight: bold;}
.pagination .page-break {border: medium none; background:none transparent; color:#333;}
-->
</style>
|
使用方法
代码如下 |
复制代码 |
$pageNo = $_GET['pageNo'];
if(empty($pageNo)){
$pageNo = 1;
}
//分页数据
$pageData = News::getNewsPage($pageNo,$pageSize);
//取得总行数
$count = News::getNewsCount();
//创建分页器
$p = new PageView($count['0']['TOTAL'],$pageSize,$pageNo,$pageData);
//生成页码
$pageViewString = $p->echoPageAsDiv();
|
下面再介绍一个分页类
代码如下 |
复制代码 |
<?php
class SubPages{
private $each_disNums;//每页显示的条目数
private $nums;//总条目数
private $current_page;//当前被选中的页
private $sub_pages;//每次显示的页数
private $pageNums;//总页数
private $page_array = array();//用来构造分页的数组
private $subPage_link;//每个分页的链接
private $subPage_type;//显示分页的类型
/*
__construct是SubPages的构造函数,用来在创建类的时候自动运行.
@$each_disNums 每页显示的条目数
@nums 总条目数
@current_num 当前被选中的页
@sub_pages 每次显示的页数
@subPage_link 每个分页的链接
@subPage_type 显示分页的类型
当@subPage_type=1的时候为普通分页模式
example: 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
当@subPage_type=2的时候为经典分页样式
example: 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
*/
function __construct($each_disNums,$nums,$current_page,$sub_pages,$subPage_link,$subPage_type){
$this->each_disNums=intval($each_disNums);
$this->nums=intval($nums);
if(!$current_page){
$this->current_page=1;
}else{
$this->current_page=intval($current_page);
}
$this->sub_pages=intval($sub_pages);
$this->pageNums=ceil($nums/$each_disNums);
$this->subPage_link=$subPage_link;
$this->show_SubPages($subPage_type);
//echo $this->pageNums."--".$this->sub_pages;
}
/*
__destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。
*/
function __destruct(){
unset($each_disNums);
unset($nums);
unset($current_page);
unset($sub_pages);
unset($pageNums);
unset($page_array);
unset($subPage_link);
unset($subPage_type);
}
/*
show_SubPages函数用在构造函数里面。而且用来判断显示什么样子的分页
*/
function show_SubPages($subPage_type){
if($subPage_type == 1){
$this->subPageCss1();
}elseif ($subPage_type == 2){
$this->subPageCss2();
}
}
/*
用来给建立分页的数组初始化的函数。
*/
function initArray(){
for($i=0;$i<$this->sub_pages;$i++){
$this->page_array[$i]=$i;
}
return $this->page_array;
}
/*
construct_num_Page该函数使用来构造显示的条目
即使:[1][2][3][4][5][6][7][8][9][10]
*/
function construct_num_Page(){
if($this->pageNums < $this->sub_pages){
$current_array=array();
for($i=0;$i<$this->pageNums;$i++){
$current_array[$i]=$i+1;
}
}else{
$current_array=$this->initArray();
if($this->current_page <= 3){
for($i=0;$i<count($current_array);$i++){
$current_array[$i]=$i+1;
}
}elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1 ){
for($i=0;$i<count($current_array);$i++){
$current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;
}
}else{
for($i=0;$i<count($current_array);$i++){
$current_array[$i]=$this->current_page-2+$i;
}
}
}
return $current_array;
}
/*
构造普通模式的分页
共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
*/
function subPageCss1(){
$subPageCss1Str="";
$subPageCss1Str.="共".$this->nums."条记录,";
$subPageCss1Str.="每页显示".$this->each_disNums."条,";
$subPageCss1Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
if($this->current_page > 1){
$firstPageUrl=$this->subPage_link."1";
$prewPageUrl=$this->subPage_link.($this->current_page-1);
$subPageCss1Str.="[<a href='$firstPageUrl'>首页</a>] ";
$subPageCss1Str.="[<a href='$prewPageUrl'>上一页</a>] ";
}else {
$subPageCss1Str.="[首页] ";
$subPageCss1Str.="[上一页] ";
}
if($this->current_page < $this->pageNums){
$lastPageUrl=$this->subPage_link.$this->pageNums;
$nextPageUrl=$this->subPage_link.($this->current_page+1);
$subPageCss1Str.=" [<a href='$nextPageUrl'>下一页</a>] ";
$subPageCss1Str.="[<a href='$lastPageUrl'>尾页</a>] ";
}else {
$subPageCss1Str.="[下一页] ";
$subPageCss1Str.="[尾页] ";
}
echo $subPageCss1Str;
}
/*
构造经典模式的分页
当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
*/
function subPageCss2(){
$subPageCss2Str="";
$subPageCss2Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
if($this->current_page > 1){
$firstPageUrl=$this->subPage_link."1";
$prewPageUrl=$this->subPage_link.($this->current_page-1);
$subPageCss2Str.="[<a href='$firstPageUrl'>首页</a>] ";
$subPageCss2Str.="[<a href='$prewPageUrl'>上一页</a>] ";
}else {
$subPageCss2Str.="[首页] ";
$subPageCss2Str.="[上一页] ";
}
$a=$this->construct_num_Page();
for($i=0;$i<count($a);$i++){
$s=$a[$i];
if($s == $this->current_page ){
$subPageCss2Str.="[<span style='color:red;font-weight:bold;'>".$s."</span>]";
}else{
$url=$this->subPage_link.$s;
$subPageCss2Str.="[<a href='$url'>".$s."</a>]";
}
}
if($this->current_page < $this->pageNums){
$lastPageUrl=$this->subPage_link.$this->pageNums;
$nextPageUrl=$this->subPage_link.($this->current_page+1);
$subPageCss2Str.=" [<a href='$nextPageUrl'>下一页</a>] ";
$subPageCss2Str.="[<a href='$lastPageUrl'>尾页</a>] ";
}else {
$subPageCss2Str.="[下一页] ";
$subPageCss2Str.="[尾页] ";
}
echo $subPageCss2Str;
}
}
?>
|
调用方法
代码如下 |
复制代码 |
<?php
require_once("SubPages.php");
//每页显示的条数
$page_size=20;
//总条目数
$nums=1024;
//每次显示的页数
$sub_pages=10;
//得到当前是第几页
$pageCurrent=$_GET["p"];
//if(!$pageCurrent) $pageCurrent=1;
$subPages=new SubPages($page_size,$nums,$pageCurrent,$sub_pages,"test.php?p=",2);
?>
|
本文章是一个自己的对CI框架的学习笔记,用一个完整的搜索,查找并且实现分页的程序给大家参考参考。
举个按关键词搜索结果分页的例子,
1.视图HTML
代码如下 |
复制代码 |
<div id="body">
<form action="/index.php/search/index/" method="get">
<p>请输入书名、作者、出版社中的一个或多个来查询。</p>
<p><input type="text" name="s" value="" size="64" /> <input type="submit" value="搜索" /></p>
</form>
</div>
|
即表单提交到名叫search的controller和名叫index的方法, 其中包含了一些动态参数,不是纯列表,故相对比较复杂,后面会提到。
代码如下 |
复制代码 |
public function index() {
$keyword = $this->input->get ( 's' );
$offset = $this->input->get ( 'offset' );
if (empty ( $offset )) {
$offset = 0;
}
if (! empty ( $keyword )) {
$this->load->model ( 'book_model' );
$this->load->library ( 'pagination' );
$per_page = 10;
$config ['num_links'] = 5;
$config ['base_url'] = '/index.php/' . $this->router->class . '/' . $this->router->method . '/?s=' . $keyword;
$config ['per_page'] = $per_page;
$config ['total_rows'] = $this->Book_Model->find_by_name ( $keyword, NULL, NULL, true );
$config ['page_query_string'] = false;
$config ['query_string_segment'] = 'offset'; //重新定义记录起始位置的参数名,默认为per_page
$this->pagination->initialize ( $config );
$data ['books'] = $this->Book_Model->find_by_name ( $keyword, $per_page, $offset );
$this->load->view ( 'search', $data );
} else {
$this->load->view ( 'search' );
}
}
|
因为config.php中默认的enable_query_strings是false, 起始位置始终在最后,这样出来的结果类似/index.php/search/index/?s=中国/10,页码取不到,需要将此配置改为false;
3.模型
代码如下 |
复制代码 |
public function find_by_name($name, $per_page=0, $offset = 0, $is_total = false) {
if ($is_total) {//总数
$query = $this->db->query ( "select count(id) as cnt from {$this->_book} where book_name like '%{$name}%'" );
if ($query->num_rows () > 0) {
$row = $query->row ();
$ret = $row->cnt;
}
}else{//列表
$query = $this->db->query ("select * from {$this->_book} where book_name like '%{$name}%' limit {$offset}, {$per_page}");
$ret = $query->result ();
}
return $ret;
} |
header函数在php中用处很多,下面我来介绍利用php中的header函数下载文件是中文在不同浏览器筹码的解决办法。
在下载文件中加入下段代码即可解决
代码如下 |
复制代码 |
$name = rawurlencode($filename);
header("Content-type: text/plain; charset=utf-8");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$name.txt.'"');
header("Content-Transfer-Encoding: binary");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
|