PHP substr() function implements filtering with mantissa 4_PHP tutorial

WBOY
Release: 2016-07-13 10:55:34
Original
1203 people have browsed it

Today a customer wants to generate 400 phone numbers in batches. The starting position is 10000. At the beginning, I directly generated numbers such as 10004. However, the merchants did not like this number, so they said that the filtering number ends in 4. Let me think about it. This can be achieved by using substr($str,-1)==4. See the code below.

 代码如下 复制代码
include("cn.php"); //数据库连接类,这里不说了自己去找找吧。
$Db = new Db();
$sql ="select * from 表名 where isnew=1";
$query = $Db->query( $sql );
$rs = $Db->fetch( $query ,0);
$i=10000;
foreach( $rs as $v => $vv )
{
 if( substr($i,-1)==4 )
 {
  $i++;
 }
 $sql ="update 表名 set txtiphone= '$i' where id=".$vv['id'];
 if( $Db->query( $sql ) )
 {
  $i++;
 }
}
echo '批量更新完成';

About the substr() function

Grammar

substr(<em>string</em>,<em>start</em>,<em>length</em>)
Copy after login

Parameters Description
参数 描述
string 必需。规定要返回其中一部分的字符串。
start

必需。规定在字符串的何处开始。

  • 正数 - 在字符串的指定位置开始
  • 负数 - 在从字符串结尾的指定位置开始
  • 0 - 在字符串中的第一个字符处开始
length

可选。规定要返回的字符串长度。默认是直到字符串的结尾。

  • 正数 - 从 start 参数所在的位置返回
  • 负数 - 从字符串末端返回
string Required. Specifies a part of the string to be returned.

start

Required. Specifies where in the string to begin.
  • Positive number - starts at the specified position in the string
  • Negative number - starts at the specified position from the end of the string
  • 0 - Start at the first character in the string
length

Optional. Specifies the length of the string to be returned. The default is until the end of the string.

  • Positive number - returned from the location of the start parameter
  • Negative number - returned from the end of the string
Please indicate the source when reprinting original tutorials on this site: http://www.bKjia.c0m/phper/php.html
http://www.bkjia.com/PHPjc/632231.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632231.htmlTechArticleToday a customer wanted to generate 400 phone calls in batches. The starting position was No. 10000. When I started to generate it directly, something like this appeared. But businessmen don’t like the number 10004, so the filtering mantissa is...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template