栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP中一些可以替代正则表达式函数的字符串操作函数

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP中一些可以替代正则表达式函数的字符串操作函数

0x01:根据预定义的字符对字符串进行词法分析

复制代码 代码如下:


$info="lv chen yang|Hello:world&757104454@qq.com";
//定义界定符,包括(|)(:)( )(&)
$tokens="|:& ";
$tokened=strtok($info, $tokens);
while ($tokened)
{
 echo "Element:$tokened
";
 //连续调用strtok()函数,完成对整个字符串的词法分析
 $tokened=strtok($tokens);
}
?>

0x02:根据预定义的定界符分解字符串

复制代码 代码如下:

$summary="
   In the latest installment of the ongoing Developer.com PHP series.
   I discuss the many improvements and addtions to
   PHP object-oriented architecture.
   ";
echo "
";
$words=explode(' ', strip_tags($summary));
echo "This sentence's lenght is:".sizeof($words);

?>

0x03:将数组转换成字符串

复制代码 代码如下:

$citys=array("Chengdu","Chongqing","Beijing","Shanghai","Guangzhou");
$citystring=implode("|", $citys);
echo $citystring;
?>

0x04:解析复杂的字符串

复制代码 代码如下:

$substr="index.html";
$log=<< 192.168.1.1:/www/htdocs/index.html:[2013/06/26:13:25:10]
192.168.1.2:/www/htdocs/index.html:[2013/06/26:13:27:16]
192.168.1.3:/www/htdocs/index.html:[2013/06/26:13:28:45]
logfile;
echo "
";
//$substr在log中首次出现的位置是什么
$pos=strpos($log, $substr);
//查找行结束的数值位置
$pos1=strpos($log,"n",$pos);
//计算时间戳的开始
$pos=$pos+strlen($substr)+1;
//检索时间戳
$timestamp=substr($log, $pos,$pos1-$pos);
echo "The file index.html was first accessed on: $timestamp
";

?>

0x05:找到字符串最后一次出现的位置

复制代码 代码如下:

$limit=100;
$summary="In the latest installment of the ongoing Developer.com PHP series.
   I discuss the many improvements and addtions to
   PHP object-oriented architecture. ";
if(strlen($summary)>$limit)
 $summary=substr($summary, 0,strrpos(substr($summary, 0,$limit)," "))."...";
echo  $summary;
?>

0x06:用另外一个字符串替换字符串的所有实例

复制代码 代码如下:

$email="lvchenyang@live.cn";
$email=str_replace("@", "(at)", $email);
echo "
".$email;
?>

0x07:获取字符串的一部分

复制代码 代码如下:

$url="lvchenyang@live.cn";
echo "
".ltrim(strstr($url, "@"),"@");
?>

0x08:根据预定义的便宜返回字符串的一部分

复制代码 代码如下:

$str="lvchenyang";
echo "
".substr($str, 2,4);
//output: chen
?>

0x09:确定字符串出现的频率

复制代码 代码如下:

$talk=<< I am acertain that we could dominate mindshare in this space with
our new product, extablishing a true synergy beteen the marketing
and product development teams. We'll own this space in thress months.
talk;
echo "
";
$sentencearray=explode(" ", $talk);
foreach ($sentencearray as $item)
{
 echo "The word $item appears(".substr_count($talk, $item).")times
";
}
?>

0x10:用另一个字符串替换一个字符串的一部分

复制代码 代码如下:

$phonenum="15926841384";
echo "
".substr_replace($phonenum, "****", 3,4);
?>

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/46969.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号