php ereg_replace函数

 更新时间:2016年11月25日 15:52  点击:1888

$string = "this is a test";
echo str_replace(" is", " was", $string);
echo ereg_replace("( )is", "1was", $string); 其中1就是第一个括号中空格
echo ereg_replace("(( )is)", "2was", $string); 其中2就是第二个括号的空格
上面三行也就是把" is"替换为" was";都有空格的。

ereg_replace ( string pattern, string replacement, string string)
也就是说如果pattern中带有()得字符串(如;括号中有空格),那你的replacement就可以使用如1的字符串,那这个1就给可以用你第1个括号中字符串来替换,如果是2那就用第2个括号中的字符串替换.
括号从左到右,以左括号为准.

下面那个是去掉url中的如&page=1

符串比对解析并取代。

语法: string ereg_replace(string pattern, string replacement, string string);

返回值: 字符串

函数种类: 资料处理


 
 
内容说明


本函数以 pattern 的规则来解析比对字符串 string,欲取而代之的字符串为参数 replacement。返回值为字符串类型,为取代后的字符串结果。


 
 
使用范例


ken@freebsdrocks.com 在 16-mar-1999 提出的例子。

<?php教程
$text = 'this is a {1} day, not {2} and {3}.';
$daytype = array( 1 => 'fine',
                  2 => 'overcast',
                  3 => 'rainy' );
while (ereg ('{([0-9]+)}', $text, $regs)) {
  $found = $regs[1];
  $text = ereg_replace("{".$found."}", $daytype[$found], $text);
}
echo "$textn";
// this is a fine day, not overcast and rainy.
?>

ken@freebsdrocks.com 并同时提出具有相同功能的perl 程序范例如下:

$text = 'this is a {1} day, not {2} and {3}.';
%daytype = ( 1 => 'fine',
             2 => 'overcast',
             3 => 'rainy' );
$text =~ s/{(d+)}/$daytype{$1}/eg;
print "$textn";

asp教程/asp.net教程/php教程实现301重定向代码,一般我们做301是全用iis,apache来实现,在没有操作服务器的时我们再用脚本来实现,代码如下。
1、asp下的301重定向代码

<%@ language=vbscript %>
<%
response.status=”301 moved permanently”
response.addheader “location”, “106/”
%>

2、asp.net下的301重定向代码

<script runat=”server”>
private void page_load(object sender, system.eventargs e)
{
response.status = “301 moved permanently”;
response.addheader(”location”,” 106/“);
}
</script>

3、php下的301重定向代码

header(”http/1.1 301 moved permanently”);
header(”location: 106/”);
exit();

 

php教程 效率的字符串处理方法

<?php
$str = array(
"helloworld",
"howareyou",
"cpufrequency",
"windows7ready",
"newedition2",
"downloadurllist",
"heisasuperhero",
);

//你的解决方法


/*
正确添加空格后应为:
"helloworld" = "hello world"
"howareyou" = "how are you"
"cpufrequency" = "cpu frequency"
"windows7ready" = "windows 7 ready"
"newedition2" = "new edition 2"
"downloadurllist" = "download url list"
"heisasuperhero" = "he is a super hero"
*/

?>

程序代码

function transfer($input) {
        $newarray = array();
        foreach($input as $i) {
                $arr = str_split($i);
                $word = '';
                foreach($arr as $a) {
                        $ascii = ord($a);
                        $lastword = substr($word, -1);
                        $ascii_1 = ord($lastword);
                        $lastword_ = substr($word, -2, 1);
                        if($ascii > 64 && $ascii < 91) {
                                if($ascii_1 > 96 && $ascii_1 < 122) {
                                        $word .= ' '.$a;
                                } else {
                                        $word .= $a;
                                }
                        } elseif($ascii > 96 && $ascii < 122) {
                                if($ascii_1 > 64 && $ascii_1 < 91) {
                                        if(strlen($word) == 1) {
                                                $word .= $a;
                                        } else {
                                                if(ord($lastword_) == 32) {
                                                        $word .= $a;
                                                } else {
                                                        $word = substr($word, 0, -1).' '.$lastword.$a;
                                                }
                                        }
                                } else {
                                        $word .= $a;
                                }
                        } else {
                                if(strlen($word) == 0) {
                                        $word .= $a;
                                } else {
                                        $word .= ' '.$a;
                                }
                        }
                }
                $newarray[$i] = $word;
        }
        return $newarray;
}

print_r(transfer($str));

在php教程使用curl时必须先在php.ini中开启extension=php_curl.dll前面的;去了,才行哦,curl是php中一款内置的浏览器,它可以模仿用户浏览信息进行网站浏览,等下面来看一实例。


function postpage($url)
{
$response = "";
$rd=rand(1,4);
$proxy='http://221.214.27.253:808';
if($rd==2) $proxy='http://222.77.14.56:8088';
if($rd==3) $proxy='http://202.98.123.126:8080';
if($rd==4) $proxy='http://60.14.97.38:8080';
if($url != "") {
$ch = curl_init($url);
curl_setopt($ch, curlopt_header, 0);
curl_setopt($ch, curlopt_returntransfer, true);
curl_setopt($ch, curlopt_proxy, $proxy);
$response = curl_exec($ch);
if(curl_errno($ch)) $response = "";
curl_close($ch);
}
return $response;
}

curl包括部份函数

curl_close — close a curl session
curl_copy_handle — copy a curl handle along with all of its preferences
curl_errno — return the last error number
curl_error — return a string containing the last error for the current session
curl_exec — perform a curl session
curl_getinfo — get information regarding a specific transfer
curl_init — initialize a curl session
curl_multi_add_handle — add a normal curl handle to a curl multi handle
curl_multi_close — close a set of curl handles
curl_multi_exec — run the sub-connections of the current curl handle
curl_multi_getcontent — return the content of a curl handle if curlopt_returntransfer is set
curl_multi_info_read — get information about the current transfers
curl_multi_init — returns a new curl multi handle
curl_multi_remove_handle — remove a multi handle from a set of curl handles
curl_multi_select — wait for activity on any curl_multi connection
curl_setopt_array — set multiple options for a curl transfer
curl_setopt — set an option for a curl transfer
curl_version — gets curl version information

一、   概述

php教程-excelreader   是一个读取 excel xsl   文件内容的一个 php   类。

它的下载网址:  http://sourceforge.net/projects/phpexcelreader/

本博客下载地址:phpexcelreader.zip

测试用excel文件:测试.xls

文件名: phpexcelreader.zip

包含两个必需文件: oleread.inc   、 reader.php   。其它文件是一个应用例子 ,   自述文件等

二、   文件使用

首先 ,   包含 reader   类文件: require_once " reader.php";

新建一个实例: $xl_reader= new spreadsheet_excel_reader ( );

设定编码信息: $xl_reader ->setoutputencoding('utf-8');//不设定可能会是乱码 要和网页显示编码一致

读取 excel   文件信息: $xl_reader->read("filename.xls");

它将导出 excel   文件中所有可以识别的数据存储在一个对象中。数据存储在 2   个数组中,目前没有提供方法 /   函数访问这些数据 .   可以像下面这样简单的使用数组名。

sheets   数组包含了读取入对象的大量数据。它将导出 excel   文件中所有可以识别的数据存储在一个 2   维数组中 $xl_reader->sheets[x][y]   。 x  为文档中的表序号, y  是以下的某个参数 :

①        numrows -- int --  表的行数

例如: $rows = $xl_reader->sheets[0]['numrows']

②        numcols -- int --  表的列数

例如: $cols = $xl_reader->sheets[0]['numcols']

③        cells -- array --  表的实际内容。是一个 [row][column]   格式的 2   维数组

    例如: $cell_2_4 = $xl_reader->sheets[0]['cells'][2][4] //   行 2,   列 4   中的数据

④        cellsinfo -- array --  表格中不同数据类型的信息。每个都包含了表格的原始数据和类型。这个数组包含 2   部分: raw --  表格原始数据; type --  数据类型。

注:只显示非文本数据信息。

例如: $cell_info = $xl_reader[0]['cellsinfo'][2][4]

$cell_info['raw'] is the raw data from the cell

$cell_info['type'] is the data type

$xl_reader->sheets

[!--infotagslink--]

相关文章

  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • 源码分析系列之json_encode()如何转化一个对象

    这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
  • php中去除文字内容中所有html代码

    PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
  • index.php怎么打开?如何打开index.php?

    index.php怎么打开?初学者可能不知道如何打开index.php,不会的同学可以参考一下本篇教程 打开编辑:右键->打开方式->经文本方式打开打开运行:首先你要有个支持运行PH...2017-07-06
  • PHP函数分享之curl方式取得数据、模拟登陆、POST数据

    废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07
  • php中的foreach函数的2种用法

    Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
  • C语言中free函数的使用详解

    free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
  • PHP函数strip_tags的一个bug浅析

    PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数...2014-05-31
  • PHP加密解密函数详解

    分享一个PHP加密解密的函数,此函数实现了对部分变量值的加密的功能。 加密代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数 srand(...2015-10-30
  • SQL Server中row_number函数的常见用法示例详解

    这篇文章主要给大家介绍了关于SQL Server中row_number函数的常见用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08
  • PHP中func_get_args(),func_get_arg(),func_num_args()的区别

    复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...2013-10-04