两种php 字符串替换函数
本文章主要讲到两种php 字符串替换函数 str_replace与substr_replace前一种是替换所有haystace中的needle,并返回haystace替换后的结果,而substr_replace替换字符串string中的一部分具体哪能一部分则取决于起始位置和可选参数length的值
最常用str_replace()
函数原型:
mixed str_replace(mixed needle,mixed new_needle,mixed haystace[,int &count])
函数用new_needle替换所有haystace中的needle,并返回haystace替换后的结果.可选的第四个参数count,它包含要执行的替换操作次数.
例:
代码如下 | 复制代码 |
<?php function n2n() { =array('1','2','3','4','5','6','7','8','9','0'); =array("7","8","9","0","1","2","3","4","5","6"); =strlen(); for(=0;<;++){ for(=0;<10;++) if(==){ = str_replace(, , ); break; } } return ; } ="1234abc56"; echo n2n(); ?> |
函数substr_replace()则用来在给定位置中查找和替换字符串中特定的子字符串.原型:
string substr_replace(string string,string replacement,int start,int[length]);
代码如下 | 复制代码 |
<?php echo substr_replace("hello world","earth",6);//hello earth ?> |
负数时
代码如下 | 复制代码 |
<?php substr_replace('huevos','x',-2,-2); //huevxos substr_replace('huevos','x',-2,-3); //huevxos substr_replace('huevos','x',-2,-3); //huevxos ?> |
函数使用字符串replacement替换字符串string中的一部分具体哪能一部分则取决于起始位置和可选参数length的值.
start的值代表要替换字符串位置的开始偏移量.如果它为0或是一个正值,就是一个从字符串开始处计算的偏移量;如果它是一个负值,就是从字
代码如下 | 复制代码 |
int preg_match ( string pattern, string subject [, array matches [, int flags]]) |
在 subject 字符串中搜索与 pattern 给出的正则表达式相匹配的内容。
如果提供了 matches,则其会被搜索的结果所填充。$matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本,以此类推。
代码如下 | 复制代码 |
$string = 'cgjp123@163.com.cn'; |
php中检查email完整性
代码如下 | 复制代码 |
<?php if (eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]?$",?$email)) { echo "您的 e-mail 通过初步检查www.111cn.net"; } ?> |
^([a-za-z0-9_-]+)@([a-za-z0-9_-]+)(.[a-za-z0-9_-])$/
就是记录邮件地址的用户名,和邮件地址的服务器地址(形式为username@server.com之类的),在后面如果想要读取记录下来的字符串,只是需要用”转义符+记录的次序”来读取。比如”1〃就相当于第一个”[a-za-z0-9_-]+”,”2〃相当于第二个([a-za-z0-9_-]+),”3〃就是第三个(.[a-za-z0-9_-])。但是在php中,”"是一个特殊的字符,需要转义,所以”"到了php的表达式中就应该写成”1〃。
其他特殊符号:
“|”:或符号”|”和php里面的或一样,不过是一个”|”,而不是php的两个”||”!意思就是可以是某个字符或者另一个字符串,比如”/abcd|dcba/”可能匹配”abcd”或者”dcba”。
在php中有这么一个函数checkdnsrr来验证dns是否可访问来检测邮箱地址是否存在
*/
代码如下 | 复制代码 |
$email ="abc@111cn.net"; if($check_email) { return true; } else { return false; } |
/*
关于checkdnsrr函数详细说明
bool checkdnsrr ( string $host [, string $type = "mx" ] )
checkdnsrr. 检查指定网址的dns 记录
<?php
代码如下 | 复制代码 |
//this will not work //but every value other than "any" will work |
指定的参数 host 可以是网络位址 (ip address),也可以用机器名称 (domain name)。参数 type 可以省略,内定值为 mx。而参数 type 的值可为以下的其中之一:a、mx、ns、soa、ptr、cname 或 any。若找到了指定网址的 dns 字段,返回 true;若未找到指定的 dns 字段或是有错误均会返回 false。
浏览器关闭的过程中,服务器上的 session 数据并没有被销毁,因为这时候没有发送任何请求,服务器那边不会知道是否要销毁 session 的数据。浏览器再次启动重新打开页面的时候,发送的 session id 还是原来的 id,虽然你说没用到 cookie,但是这个 id 就是通过 cookie 发送的。
你可以看看 php.ini 的设置,主要看这两项:
代码如下 | 复制代码 |
session.gc_maxlifetime session.cookie_lifetime |
方法二利用onload函数
代码如下 | 复制代码 |
<body onunload="ajax()"> |
主要是购物车部分代码的讲解,和对session操作的php教程代码演示。
<?php
//
// add_item.php:
// add an item to the shopping cart.
//
session_start();
if (session_is_registered('cart')) {
session_register('cart');
}require 'lib.inc.php'; // loadproducts()
loadproducts(); // load products in $master_products_list
// make $curr_product global
$curr_product = array();// loop through all the products and pull up the product
// that we are interested in
foreach ($master_products_list as $prod_id => $product) {
if (trim($prod_id) == trim($_get[id])) {
$curr_product = $product;
}
}
// register our session
//session_register('cart');
//if(session_is_registered('cart')) echo "已经注册";
if ($_post[ordered]) { // if they have chosen the product
array_push($_session[cart][products], array(trim($_post[id]), $_post[quantity]));
$_session[cart][num_items] += $_post[quantity];
}
?><html>
<head>
<title>
<?php if ($_post[ordered]) { ?>
已经添加 <?php echo $curr_product[name]; ?> 到您的购物篮
<?php } else { ?>
添加 <?php echo $curr_product[name]; ?> 到您的购物篮
<?php } ?>
</title>
</head>
<body>
<?php if ($_post[ordered]) { ?>
<h1><?php echo $curr_product[name]; ?>
添加至购物篮成功</h1><a href="cart.php">返回</a> 商品列表页面.
<?php } else { ?>
<h1>添加 <?php echo $curr_product[name]; ?> 到您的购物篮</h1><form action="<?php echo $php_self; ?>" method="post">
商品名称: <?php echo $curr_product[name]; ?>
<br>
商品说明: <?php echo $curr_product[desc]; ?>
<br>
商品单价: rmb<?php echo $curr_product[price]; ?>
<br>
商品数量: <input type="text" size="7" name="quantity">
<input type="hidden" name="id" value="<?php echo $_get[id]; ?>">
<input type="hidden" name="ordered" value="1"><input type="submit" value="添加至购物栏">
</form>
<?php } ?>
</body>
</html>
php代码
<?php
//
// cart.php: the main file
//
session_start();require 'lib.inc.php';
//判断购物篮会话变量cart是否注册,不注册则注册cart变量
if (session_is_registered('cart')) {
session_register('cart');
}
// 如果购物篮没有初始化,则初始化购物篮
if (!isset($_session[cart][num_items])) {
$_session[cart] = array("num_items" => 0,
"products" => array());
}
// from site_lib.inc, loads the $master_products_list array
loadproducts(); //载入物品列表
?><html>
<head>
<title>演示会话跟踪的购物篮程序</title>
</head><body>
<h1>欢迎进入网上商店</h1>
<?php
if ($_session[cart][num_items]) { // if there is something to show
?>
<h2>当前在购物篮里的物品</h2>
<br>
<table border="2" cellpadding="5" cellspacing="2">
<tr>
<th>
商品名称
</th>
<th>
商品说明
</th>
<th>
单价
</th>
<th>
数量
</th>
<th>
</th>
</tr>
<?php
// loop through the products
foreach ($_session[cart][products] as $i => $product) {
$product_id = $product[0];
$quantity = $product[1];$total += $quantity *
(double)$master_products_list[$product_id][price];
?>
<tr>
<td>
<?php echo $master_products_list[$product_id][name]; ?>
</td>
<td>
<?php echo $master_products_list[$product_id][desc]; ?>
</td>
<td>
<?php echo $master_products_list[$product_id][price]; ?>
</td>
<td>
<form action="change_quant.php" method="post">
<input type="hidden" name="id" value="<?php echo $i; ?>">
<input type="text" size="3" name="quantity"
value="<?php echo $quantity; ?>">
</td>
<td>
<input type="submit" value="数量更改">
</form>
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="2" align="right">
<b>合计: </b>
</td>
<td colspan="2">
rmb:<?php echo $total; ?>
</td>
<td> </td>
</tr>
</table>
<br>
<br>
<?php
}
?><h2>商店待出售的商品</h2>
<br>
<i>
我们提供以下商品待售:
</i>
<br>
<table border="2" cellpadding="5" cellspacing="2">
<tr>
<th>
商品名称
</th>
<th>
商品说明
</th>
<th>
单价
</th>
<th>
</th>
</tr>
<?php
// show all of the products
foreach ($master_products_list as $product_id => $item) {
?>
<tr>
<td>
<?php echo $item[name]; ?>
</td>
<td>
<?php echo $item[desc]; ?>
</td>
<td>
$<?php echo $item[price]; ?>
</td>
<td>
<a href="add_item.php?id=<?php echo $product_id; ?>">
添加至购物篮
</a>
</td>
</tr>
<?php
}?>
</table>
购物车
<?php
//
// change_quant.php:
// change the quantity of an item in the shopping cart.
//
session_start();
if (session_is_registered('cart')) {
session_register('cart');
}// typecast to int, making sure we access the
// right element below
$i = (int)$_post[id];// save the old number of products for display
// and arithmetic
$old_num = $_session[cart][products][$i][1];if ($_post[quantity]) {
$_session[cart][products][$i][1] = $_post[quantity]; //change the quantity
} else {
unset($_session[cart][products][$i]); // send the product into oblivion
}// update the number of items
$_session[cart][num_items] = ($old_num >$_post[quantity]) ?
$_session[cart][num_items] - ($old_num-$_post[quantity]) :
$_session[cart][num_items] + ($_post[quantity]-$old_num);
?><html>
<head>
<title>
数量修改
</title>
</head>
<body>
<h1> 将数量: <?php echo $old_num; ?> 更改为
<?php echo $_post[quantity]; ?></h1>
<a href="cart.php">返回</a> 商品列表页面.
</body>
</html>
打开文件
<?php
//物品数组
$master_products_list = array();
//载入物品数据函数
function loadproducts() {
global $master_products_list;
$filename = 'products.txt';$fp = @fopen($filename, "r")
or die("打开 $filename 文件失败");
@flock($fp, 1)
or die("锁定 $filename 文件失败");//读取文件内容
while ($line = fgets($fp, 1024)) {
list($id, $name, $desc, $price) = explode('|', $line); //读取每行数据,数据以| 格开
$id = trim($id); //去掉首尾特殊符号
$master_products_list[$id] = array("name" => $name, //名称
"desc" => $desc, //说明
"price" => $price); //单价
}@fclose($fp) //关闭文件
or die("关闭 $filename 文件失败");
}
?>
相关文章
- eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
- 在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
- 这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
Python astype(np.float)函数使用方法解析
这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08- 这篇文章介绍了C#判断字符串是否数字或字母的实例,有需要的朋友可以参考一下...2020-06-25
- 这篇文章主要介绍了PostgreSQL判断字符串是否包含目标字符串的多种方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-23
- 这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
- 这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 本文实例讲述了php字符串按照单词进行反转的方法。分享给大家供大家参考。具体分析如下:下面的php代码可以将字符串按照单词进行反转输出,实际上是现将字符串按照空格分隔到数组,然后对数组进行反转输出。...2015-03-15
- CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
Android开发中findViewById()函数用法与简化
findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20- 要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25
- 这篇文章主要介绍了MySQL 字符串拆分操作(含分隔符的字符串截取),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-22
- 这篇文章主要介绍了使用list stream:任意对象List拼接字符串操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-09
- strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
- 这篇文章主要介绍了C# 16 进制字符串转 int的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2020-06-25
- JS中默认中文字符长度和其它字符长度计算方法是一样的,但某些情况下我们需要获取中文字符串的实际长度,代码如下: 复制代码 代码如下: function strLength(str) { var realLength = 0, len = str.length, charCode = -1;...2014-06-07